0

The Promise.all method doesn't have an index to target a particular promise so in my case, I'm wondering if I can do this:

let promises = [Promise.resolve(true), 
                Promise.resolve(true), 
                Promise.resolve(false), 
                Promise.resolve(true)];

Promise.all(promises).then(result => {
    console.log(result);
    // [true, true, false, true]

    let failed = result.findIndex(r => !r);
    console.log(promises[failed]);

});

Now this is assuming the order is always maintained, i.e the result array is always in the same sequence as the input array?

eozzy
  • 66,048
  • 104
  • 272
  • 428

1 Answers1

2

When using Promise.all, the array returned will always be in the same order as the array provided, so you can safely use a set index.

Icehorn
  • 1,247
  • 8
  • 15
  • How do you know? Is there an authoritative reference for this? – eozzy Jan 07 '19 at 05:51
  • 1
    The MDN web docs are a good source of information for these types of questions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – Icehorn Jan 07 '19 at 06:01