0
Hours
0
Minutes
0
Seconds
You need Login/Signup to run/submit the code.

How to implement Promise.all? | Promise Polyfills | Advanced JavaScript Interview Question

@Yomesh Gupta
311

In this question, you need to implement the Promise.all from scratch. As per the MDN,

The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.

Example

function init() {
    return Promise.resolve(1);
}

const promises = [
    init(),
    init(),
    init()
];

Promise.all(promises).then(values => {
    // [1, 1, 1]
    console.log(values);
});

Submission

Please start the timer before starting and finish your solution within 45 mins. Share your solution with us on Twitter or LinkedIn.

Loading IDE...