Removing nested promises

From every then callback, you will need to return the new promise: exports.viewFile = function(req, res) { var fileId = req.params.id; boxContentRequest(‘files/’ + fileId + ‘/content’, req.user.box.accessToken) .then(function(response) { return boxViewerRequest(‘documents’, {url: response.request.href}, ‘POST’); }) .then(function(response) { return boxViewerRequest(‘sessions’, {document_id: response.body.id}, ‘POST’); }) .then(function(response) { console.log(response); }); }; The promise that is returned by the … Read more

Async function returning promise, instead of value

Async prefix is a kind of wrapper for Promises. async function latestTime() { const bl = await web3.eth.getBlock(‘latest’); console.log(bl.timestamp); // Returns a primitive console.log(typeof bl.timestamp.then == ‘function’); //Returns false – not a promise return bl.timestamp; } Is the same as function latestTime() { return new Promise(function(resolve,success){ const bl = web3.eth.getBlock(‘latest’); bl.then(function(result){ console.log(result.timestamp); // Returns a … Read more

Is it bad practice to have a constructor function return a Promise?

Yes, it is a bad practise. A constructor should return an instance of its class, nothing else. It would mess up the new operator and inheritance otherwise. Moreover, a constructor should only create and initialize a new instance. It should set up data structures and all instance-specific properties, but not execute any tasks. It should … Read more

Wait until all promises complete even if some rejected

Update, you probably want to use the built-in native Promise.allSettled: Promise.allSettled([promise]).then(([result]) => { //reach here regardless // {status: “fulfilled”, value: 33} }); As a fun fact, this answer below was prior art in adding that method to the language :] Sure, you just need a reflect: const reflect = p => p.then(v => ({v, status: … Read more