I can't understand what's Promise.race
behavior. Does it stop after the very first promise instance in an array of promises is resolved (rejected), ignoring all the rest?
My goal is to execute all promises in an array. In any sequence but all should be executed.
An example. Lets say that array_of_urls
has 10 urls. Will Promise.race
execute all 10 promises, in any sequence one by one, or will it stop after the very first?
.then(array_of_urls => {
// array_of_urls == [10 urls]
let array_of_promises = array_of_urls.map((url) => {
return fetch(url).then(res => {
return res;
});
});
return Promise.race(array_of_promises);
})
.then(each_and_every_result => {
// What does happen here? Will this `then` callback be called 10 times or just one?
})
Do Promise
s have methods to execute all?
PS:
MDN doesn't explain if all will be executed or it will stop:
The race function returns a Promise that is settled the same way as the first passed promise to settle. It resolves or rejects, whichever happens first.
UPD:
Sorry, didn't explained it correct. I do need to execute all promises in an array but not the way Promise.all
does it. Yes, Promise.all
executes all, but it waits until all of them executed, collecting their results and then returns one array with all their results in it. And I need to call a cirtain callback every time one of those 10 is fullfilled. That is without waithig all of them to be collescted by Promise.all
.
I can't understand what's Promise.race
behavior. Does it stop after the very first promise instance in an array of promises is resolved (rejected), ignoring all the rest?
My goal is to execute all promises in an array. In any sequence but all should be executed.
An example. Lets say that array_of_urls
has 10 urls. Will Promise.race
execute all 10 promises, in any sequence one by one, or will it stop after the very first?
.then(array_of_urls => {
// array_of_urls == [10 urls]
let array_of_promises = array_of_urls.map((url) => {
return fetch(url).then(res => {
return res;
});
});
return Promise.race(array_of_promises);
})
.then(each_and_every_result => {
// What does happen here? Will this `then` callback be called 10 times or just one?
})
Do Promise
s have methods to execute all?
PS:
MDN doesn't explain if all will be executed or it will stop:
The race function returns a Promise that is settled the same way as the first passed promise to settle. It resolves or rejects, whichever happens first.
UPD:
Sorry, didn't explained it correct. I do need to execute all promises in an array but not the way Promise.all
does it. Yes, Promise.all
executes all, but it waits until all of them executed, collecting their results and then returns one array with all their results in it. And I need to call a cirtain callback every time one of those 10 is fullfilled. That is without waithig all of them to be collescted by Promise.all
.
-
"The
Promise.race(iterable)
method returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise." You're looking forPromise.all(iterable)
: ThePromise.all(iterable)
method returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects. – Andreas Commented Oct 29, 2016 at 10:47 -
Promises are not cancellable, so, even in a
race
, they will all settle (fulfilled or rejected) - just like a foot race, everybody keeps going after the first person passes the finish line - but therace
function simply settles to the first settled promise in the passed in iterable – Jaromanda X Commented Oct 29, 2016 at 10:57 - @JaromandaX, Sorry, didn't explained it correct. Please see my update in the question – Green Commented Oct 29, 2016 at 11:22
- Sorry, didn't explained it correct. Please see my update in the question @Andreas – Green Commented Oct 29, 2016 at 11:23
-
A promise can only be settled once - so, no, your last
.then
wont be called 10 times - what you need to do is call your callback in the.then
of thefetch
in the.map
before youreturn res
– Jaromanda X Commented Oct 29, 2016 at 11:28
1 Answer
Reset to default 10Promises are merely a notification system for asynchronous operations. They don't control the async operations at all. In fact, the opposite is true, the async operations control the promises. So, whether you use Promise.race()
or Promise.all()
or some other scheme with your promises does not affect the underlying asynchronous operations at all. It only affects which kind of notification you get about the pletion of the asynchronous operations.
So, when you use Promise.all()
you get a notification when either all the async operations have pleted successfully or when one has an error. If one has an error, all the other async operations are still running and will run to their normal pletion. Only your Promise.all(...).then()
handler will be affected.
When you use Promise.race()
, you are just getting a notification when the first async operation finishes. The other async operations will still run to their normal pletion.
If you want to be notified when each individual async operation pletes, then you just attach a .then()
handler to each individual promise and those .then()
handlers are not influenced at all by whether you also have a Promise.all()
or Promise.race()
on the array of promises - that would just be another notification.
I find it works best to think of promises as a notification mechanism for asynchronous operations. They don't control the asynchronous operations at all once they are started. instead, they provide structured notification about what happens with the pletion of the asynchronous operations.
// What does happen here? Will this
then
callback be called 10 times or just one?
Promises only ever resolve or reject once so your Promise.all(...).then()
or Promise.race(...).then()
handler will only ever be called once.
Will Promise.race execute all 10 promises, in any sequence one by one, or will it stop after the very first?
By the time you call Promise.race()
your async operations have already been started and they are all already underway. They will all be executed. Promise.race()
has no influence on the execution of the async operations themselves - it is just providing a notification about when the first async operation finishes.