I have multiple promises fetching different assets. I want to achieve:
then()
.Promise.all().then()
.I need to make sure 1 is excuted before 2. It does seem to work from what I have tested.
// handle promises one by one
promise1.then(results => {});
promise2.then(results => {});
// common process that should come after the above then()s
Promise.all([promise1, promise2])
.then(results => {});
But can I rely on it? Are "single" then()
always executed before a then()
on Promise.all()
?
While Promise.all is stalled because it waits for all the promises to finish, there is no guarantee that it will resolve after the .then
call on the promise that resolves last.
Instead you should try creating new promises from the .then
calls.
// handle promises one by one
const promise3 = promise1.then(results => {});
const promise4 = promise2.then(results => {});
// common process that should come after the above then()s
Promise.all([promise3, promise4])
.then(results => {});
This way you can guarantee that the Promise all will resolve after the then calls