Alan
Alan

Reputation: 1700

Why does Promise.all() allow non Promise objects?

Why does Promise.all() allow any object to be passed into as an iterable?

example (jsfiddle: https://jsfiddle.net/npqgpcud/6/):

var promise1 = "one";
var promise2 = 2222;
var promise3 = new Promise(function (fulfill, reject) {
    fulfill("three");
});

Promise.all([promise1, promise2, promise3]).then(results => {
    p1.innerHTML = results[0];
    p2.innerHTML = results[1];
    p3.innerHTML = results[2];
});

If I wanted to mimic this behaviour for a single promise, is there a recommended solution or is it recommended to use Promise.all([promise]) ?

Upvotes: 6

Views: 2159

Answers (1)

trincot
trincot

Reputation: 350996

The equivalent for one value is Promise.resolve().

You can pass it a promise, a thenable or another value. When it is:

  • An instance of the native Promise (i.e. its proto is Promise.prototype), then it returns that object (no promise is created).
  • Another thenable -- including promises from another library or extensions of native promises -- then it returns a new Promise for it (a wrapper)
  • Anything else, then it returns a new promise that is immediately resolved with the given value.

Some examples:

const wait = ms => new Promise( resolve => setTimeout(resolve, ms) );

console.log('start');
Promise.resolve(wait(1000)).then( _ => console.log('done') );
Promise.resolve(42).then( data => console.log(data) );
Promise.resolve(Promise.reject('oh no')).catch( data => console.log(data) );

The only difference that Promise.all([one value]) has, is that it will still yield an array of one value.

See the slight difference for the resolved value (42) that is displayed:

const wait = ms => new Promise( resolve => setTimeout(resolve, ms) );

console.log('start');
Promise.all([wait(1000)]).then( _ => console.log('done') );
Promise.all([42]).then( data => console.log(data) );
Promise.all([Promise.reject('oh no')]).catch( data => console.log(data) );

Upvotes: 7

Related Questions