Promise.all not waiting for Promise to resolve

Asked
Active3 hr before
Viewed126 times

9 Answers

90%

The promise on which .then is called is by definition resolved at the moment the callback is called. To return a resolved promise at that very moment does not add anything useful. To return the promise on which .then is called is just fine. You can just remove the above .then call from the chain., What is the name of this town in Italy? , Hi Mike, I have tried wrapping it in a Promise.resolve() but i get the same result. – Atomicts Aug 29 '17 at 16:31

So the first lines of your code should become:

Promise.all(
      [].concat(...this.lessons.levels.map((item, i) => {
               return item.childlevels.map((childItem, iChild) => {

The following code is useless:

            .then(() => {
               return Promise.resolve();
            })

To chain all the promises instead of using Promise.all, it is easiest if you use the async/await syntax. Something like this:

async getChildLessons() {
   this.lessons.levels.forEach((item, i) => {
      item.childlevels.map((childItem, iChild) => {
         ((i, iChild) => {
            let html = await this.horseman
               .open(childItem.url)
               .html();
            .catch((err) => {
               throw (err); // throw instead of reject
            });
            //adding some information to an array
         })(i, iChild);
      });
   })
   return this.lesson;
}
load more v
88%

If the iterable contains non-promise values, they will be ignored, but still counted in the returned promise array value (if the promise is fulfilled): , An asynchronously resolved Promise if the iterable passed contains no promises. Note, Google Chrome 58 returns an already resolved promise in this case. , The returned promise is fulfilled with an array containing all the resolved values (including non-promise values) in the iterable passed as the argument.

Promise.all(iterable);
load more v
72%

As you can see, we are passing an array to Promise.all. And when all three promises get resolved, Promise.all resolves and the output is consoled.,From the above example, it’s clear that Promise.all waits till all the promises resolve.,Promise.all is actually a promise that takes an array of promises as an input (an iterable). Then it gets resolved when all the promises get resolved or any one of them gets rejected.

Let’s see it in code:

Promise.all([Promise1, Promise2, Promise3])
   .then(result) => {
      console.log(result)
   })
.catch(error => console.log(`Error in promises ${error}`))
load more v
65%

But if at least one promise rejects, then allPromise rejects right away (without waiting for other promises to resolve) with the same reason.,However, after passing 1 second the second promise (fruits) rejects with an error new Error('Out of fruits!'). This makes allPromise reject right away with the same new Error('Out of fruits!').,Such behavior of Promise.all([...]) is named fail-fast. If at least one promise in the promises array rejects, then the promise returned by allPromise = Promise.all([...]) rejects too — with the same reason.

const allPromise = Promise.all([promise1, promise2, ...]);
load more v
75%

What is Promise.all()?,Let us see an example of Promise.all():, How to find duplicates in an array using JavaScript

const promise = Promise.all([Promise1, Promise2, Promise3, ...]);

// run all promises in parallel
promise.then(result => console.log(result))
   .catch(error => console.error(`Error: ${error}`));
load more v
40%

The solution is to wrap all in a await Promise.all() call, like this:,I want to start both first, then I want to wait until both finished. Not a millisecond more.,Sometimes we need to wait for a promise to resolve, and we also need to wait for another promise to resolve.

const values = await store.getAll()
const keys = await store.getAllKeys()
load more v
22%

I added the 'almost' prototype to my promises, using this technique, nice solution.,Instead of using Promise.all() , you can use Promise.allSettled(). Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled,Inspired from your examples, I created a small library https://www.npmjs.com/package/promise-all-always

Promise.almost = r => Promise.all(r.map(p => p.catch ? p.catch(e => e) : p));
60%

Pretag
 Pretag team - issue, fix, solve, resolve
48%

Before I begin: This is not a full tutorial on the keywords mentioned here. This is an essay on using Promise.all more effectively. Google is your friend.,So, we introduce Promise.all. Promise.all waits for an array of promises to resolve before continuing. So, if we change our code to use Promise.all instead, it'd look like this:,Here, we use Array.map to create a array of promises, and then we await that array of promises with Promise.all again.

Here's an example

function main() {
   return doSomethingAsync('Foo').then(result1 => {
      return doSomethingElseAsync('Foo').then(result2 => {
         // Now that I have my items, call the final step
         return finallySomethingAsync(result1, result2);
      });
   });
}
load more v

Other "undefined-undefined" queries related to "Promise.all not waiting for Promise to resolve"