I've been only using bluebird for a few days but I want to go over all my old code and promisify
it :)
My problem is that I still don't fully grasp the flow of then()
commands.
Consider these two blocks:
A
methodThatReturnsAPromise().then(task2).then(task3);
B
var promise = methodThatReturnsAPromise(); promise.then(task2) promise.then(task3);
in scenario A task3
will get the result of task2
? In B they all get the result of the first promise?
How does the second one differ from running Promise.all
from bluebird?
How do these A/B/Promise.all
differ when it comes to using the catch
method (where do I put it).
Sorry it's a bunch of questions in one.
Integration With Async/Await Unfortunately there is no way to get async functions to return Bluebird promises. Even if you set global. Promise = require('bluebird'); , async functions will still return native promises.
You can use the async/await syntax or call the . then() method on a promise to wait for it to resolve. Inside of functions marked with the async keyword, you can use await to wait for the promises to resolve before continuing to the next line of the function.
map. Given a finite Iterable (arrays are Iterable ), or a promise of an Iterable , which produces promises (or a mix of promises and values), iterate over all the values in the Iterable into an array and map the array to another using the given mapper function.
js 10, Bluebird promises are 20% faster than native promises and consume 40% less memory. Similarly, Bluebird's performance exceeds that of promises managed with async/await.
Welcome to the wonderful world of promises.
then
works in your exampleYour assertion in 1
is correct. We can simulate a promise resolving in Bluebird using Promise.resolve
on a value.
Let's show this:
Let's get a function that returns a promise:
function foo(){ return Promise.resolve("Value"); } foo().then(alert);
This short snippet will alert "Value"
as we can see.
Now, let's create two more promises, each that alert and return different values.
function task2(e){ alert("In two got " + e); return " Two "; } function task3(e){ alert("In three got " + e); return " Three "; }
So, as you can see in your first code it will indeed resolve in a chain, each with the value of the previous part.
In the second example, both task2 and task3 will get the same value and will also execute together (that is, task 3 will not wait for task 2). You can see that here.
Promise.all (or just returning an array from a then
fulfillment handler and then using .spread
) is used for waiting for multiple results to all complete. On your example, you're hooking on a single result in multiple parts.
You always put catch where you want the error to be caught. As you would normally in synchronous code. Just remember to always throw in a promise or in promisified code.
in scenario A task3 will get the result of task2? In B they all get the result of the first promise?
Yes.
How does the second one differ from running Promise.all from bluebird?
You do not fetch the results of the (parallel) tasks 2 and 3 into a new promise.
How do these A/B/Promise.all differ when it comes to using the catch method (where do I put it).
Usually you would put it on the end of the chain, except you want to catch a specific error.
promise.catch() // handles rejections of this promise promise.then(task2).catch() // handles rejections from either promise or task2 // if promise is rejected, task2 will not be executed Promise.all(promise.then(task2), promise.then(task3)).catch() // handles rejections from any. // if promise is rejected, neither task2 nor task3 will be executed // if task2 or task3 throw, the error will immediately handled // and the other task will not be affected (but its result is unavailable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With