Execute batch of promise with Promise.allSettled()

Promise.allSettled is available in node version >= 12.9


Promise.allSettled is not yet available for Node environments.

UPDATE: This feature is available as of node 12.9.0.

For older versions you can use an npm package as a workaround: es-shims/Promise.allSettled.


allSettled = function(promiseList) {
    let results = new Array(promiseList.length);

    return new Promise((ok, rej) => {

        let fillAndCheck = function(i) {
            return function(ret) {
                results[i] = ret;
                for(let j = 0; j < results.length; j++) {
                    if (results[j] == null) return;
                }
                ok(results);
            }
        };

        for(let i=0;i<promiseList.length;i++) {
            promiseList[i].then(fillAndCheck(i), fillAndCheck(i));
        }
    });
}