Promise.resolve vs new Promise(resolve)

Contrary to both answers in the comments – there is a difference. While Promise.resolve(x); is basically the same as new Promise(function(r){ r(x); }); there is a subtlety. Promise returning functions should generally have the guarantee that they should not throw synchronously since they might throw asynchronously. In order to prevent unexpected results and race conditions … Read more

How to extract data out of a Promise

NO you can’t get the data synchronously out of a promise like you suggest in your example. The data must be used within a callback function. Alternatively in functional programming style the promise data could be map()ed over. If your are OK using async/await (you should it’s awesome) then you can write code that looks … Read more

How to promisify Node’s child_process.exec and child_process.execFile functions with Bluebird?

I would recommend using standard JS promises built into the language over an additional library dependency like Bluebird. If you’re using Node 10+, the Node.js docs recommend using util.promisify which returns a Promise<{ stdout, stderr }> object. See an example below: const util = require(‘util’); const exec = util.promisify(require(‘child_process’).exec); async function lsExample() { try { … Read more

Placement of catch BEFORE and AFTER then

So, basically you’re asking what is the difference between these two (where p is a promise created from some previous code): return p.then(…).catch(…); and return p.catch(…).then(…); There are differences either when p resolves or rejects, but whether those differences matter or not depends upon what the code inside the .then() or .catch() handlers does. What … Read more

When would someone need to create a deferred?

Does there exist a situation where it would be necessary (or just better somehow) to use a deferred? There is no such situation where a deferred is necessary. “Better” is a matter of opinion so I won’t address that here. There’s a reason that the ES6 promise specification does not have a deferred object. You … Read more

tech