Promise.all([]).then((res) => { console.log('all'); }); Promise.race([]).then((res) => { console.log('race'); });
Promise.all(iterable) 允许在 iterable 中使用 non-promise 的“常规”值
通常,Promise.all(...) 接受含有 promise 项的可迭代对象(大多数情况下是数组)作为参数。但是,如果这些对象中的任何一个不是 promise,那么它将被“按原样”传递给结果数组。
例如,这里的结果是 [1, 2, 3]:
Promise.all([ new Promise((resolve, reject) => { setTimeout(() => resolve(1), 1000) }), 2, 3 ]).then(alert); // 1, 2, 3
所以我们可以在方便的地方将准备好的值传递给 Promise.all。