then does not accept a Promise as input, it accepts 2 functions, 1 for execution and 1 for rejection.
Reason d resolved due to the fact that the call. then with a non-convertible value (even a numeric literal - 1 or undefined ) forces you to replace the onFulfilled function with "Identity", which simply repeats the execution of any value that was allowed in the previous step. See PerformPromiseThen
Try it like this:
//promises that are never resolved nor rejected var a = function() { return new Promise(function(r,re){}); }; var b = function() { return new Promise(function(r,re){}); }; var c = function() { return new Promise(function(r,re){}); }; // or simply, a = b = c after setting the function for c var d = [a, b, c].reduce(function (previousPromise, fn) { return previousPromise.then(fn, /* not passing a rejection handler... */); }, Promise.resolve());
Or alternatively ...
//promises that are never resolved nor rejected var a = new Promise(function(r,re){}); var b = new Promise(function(r,re){}); var c = new Promise(function(r,re){}); var d = [a, b, c].reduce(function (previousPromise, promise) { return previousPromise.then(function() {return promise;}); }, Promise.resolve());
And since you are using promises and ES6, you can be more concise:
let a = new Promise(() => {}); let b = new Promise(() => {}); let c = new Promise(() => {}); let d = [a, b, c].reduce((previousPromise, promise) => previousPromise.then(() => promise), Promise.resolve());
Amit
source share