bluebird
Convert promise to bluebird
Use Promise.resolve – it will take any thenable, like a promise from some other implementation, and assimilate it into a Bluebird promise. Keep in mind that the term “resolve” can be misleading, it does not mean the same as “fulfill” but can also follow another promise and settle to its result.
Is it possible to get stack traces across async/await boundaries using –harmony_async_await in Node 7?
Since NodeJS v12 async stack traces are enabled by default. relevant issue how async stacktraces work
Koa / Co / Bluebird or Q / Generators / Promises / Thunks interplay? (Node.js) [closed]
I’ve been working almost extensively with generators for a month now so maybe I can take a stab at this. I’ll try to keep the opinions to a minimum. Hopefully it helps clarify some of the confusion. Part of the reason for the lack of best practices and better explanations is that the feature is … Read more
Catching Errors in JavaScript Promises with a First Level try … catch
You cannot use try-catch statements to handle exceptions thrown asynchronously, as the function has “returned” before any exception is thrown. You should instead use the promise.then and promise.catch methods, which represent the asynchronous equivalent of the try-catch statement. (Or use the async/await syntax noted in @Edo’s answer.) What you need to do is to return … Read more
How to chain and share prior results with Promises [duplicate]
Edit in 2023. Now that we have async/await in every Javascript environment you execute in these days, the real answer to this is to use async/await as it vastly simplifies the collection and use of intermediate results into normal Javscript variables that can be used in any part of the sequence. See the end of … Read more
Correct way to write loops for promise.
If you really want a general promiseWhen() function for this and other purposes, then by all means do so, using Bergi’s simplifications. However, because of the way promises work, passing callbacks in this way is generally unnecessary and forces you to jump through complex little hoops. As far as I can tell you’re trying : … Read more
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
Handling multiple catches in promise chain
This behavior is exactly like a synchronous throw: try{ throw new Error(); } catch(e){ // handle } // this code will run, since you recovered from the error! That’s half of the point of .catch – to be able to recover from errors. It might be desirable to rethrow to signal the state is still … 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