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]) ?
The equivalent for one value is Promise.resolve()
.
You can pass it a promise, a thenable or another value. When it is:
Promise.prototype
), then it returns that object (no promise is created).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) );