Edward Martin
Edward Martin

Reputation: 9

Find which promises aren't resolving?

I have an array of promises promises = [p1,p2,p3] however, one or more aren't resolving and so Promise.all(promises) never finishes and just keeps waiting.

How can I console.log every time each individual promise resolves (perhaps with the array index) so that I can find the one(s) that aren't resolving?

Upvotes: 0

Views: 142

Answers (1)

lusc
lusc

Reputation: 1396

You could loop over all promises and wait for each individual promise to resolve.

const p1 = new Promise(resolve => setTimeout(resolve, 1000, 'p1'));
const p2 = new Promise(resolve => setTimeout(resolve, 3000, 'p2'));
const p3 = new Promise(resolve => {/* never resolve */});

const promises = [p1, p2, p3];

promises.forEach((p, i) => {
  p.then((value) => {console.log('Promise at index %i resolved with %o', i, value);});
});

Upvotes: 1

Related Questions