Search code examples
javascriptnode.jsecmascript-6babeljses6-promise

Promise.allSettled in babel ES6 implementation


I'm using babel to transpile my [email protected] code and I'm stuck with promises.

I need allSettled-type functionality that I could use in q and bluebird or angular.$q for example.

On babel's core-js Promise, there is no allSettled method.

Currently I'm using q.allSettled as a workaround:

import { allSettled } from 'q';

Is there something like that in babel polyfill? Alternatively, which is a good algorithm for me to try to implement?


Solution

  • Alternatively, which is a good algorithm for me to try to implement?

    1. create a new promise with an executor function
    2. use a counter/result array in the scope of the executor
    3. register a then() callback with each parent promise saving the results in the array
    4. resolve/reject promise from step 1 when counter indicates that all parent promises are done