Why passing directly Promise.all to a .then function throws an error?

I want to pass directly Promise.all to a .then function like:

function returnPromise(a) {
    return Promise.resolve(a);
}

function promiseList(arr) {
    var list =[];
    for(let i= 0; i < arr.length; i++) {
        list.push(returnPromise(arr[i]))
    }
    return list;
}

var promsie = new Promise(function(fulfill, reject) {
    fulfill(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
}); 

promsie
.then(Promise.all)
.then(console.log)

But it outputs:

(node:19707) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Promise.all called on non-object
(node:19707) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Changing it to

promsie
  .then(arr => Promise.all(arr))
  .then(console.log);

seems to work. I don’t know what’s causing the TypeError in your current code.

1 Like