翻译正在进行中。

概述

Promise.all() 方法返回一个 Promise, 在可迭代(iterable)参数中所有的 promises 都已经解决了或者当 iterable 参数不包含 promise 时, 返回解决。或者返回拒绝, 当传递的 promise 包含一个拒绝(reject)时。

语法

Promise.all(iterable);

参数

iterable
一个可迭代对象,例如一个 ArrayString。参见 iterable.

返回值

一个 Promise,它将在上述可迭代对象中的所有 Promise 被 resolve 之后被 resolve,或者在任一 Promise 被 reject 后被 reject。

  • An already resolved Promise 如果传递空的可迭代对象
  • An asynchronously resolved Promise 如果传递可迭代对象中不包含Promise. 比如, Google Chrome 58 返回一个 already resolved promise .
  • pending Promise 在其他范例中. 返回的 promise 将会 处于异步等待 resolved/rejected的状态 (as soon as the stack is empty) 当可迭代对象中包含的所有Promise被resolved时, 或者其中任何一个Promise被reject时, 异步等待结束. See the example about "Asynchronicity or synchronicity of Promise.all" below.

描述

Promise.all 是当所有给定的可迭代完成时执行 resolve,或者任何  promises 失败时执行 reject。

如果传递任何的 promises rejects ,所有的 Promise 的值立即失败,丢弃所有的其他 promises,如果它们未 resolved。如果传递任意的空数组,那么这个方法将立刻完成。

示例

使用 Promise.all

Promise.all 等待所有代码的完成(或第一个代码的失败)。

let p1 = Promise.resolve(3);
let p2 = 1337;
let p3 = new Promise((resolve, reject) => {
    setTimeout(resolve, 100, "foo");
}); 

Promise.all([p1, p2, p3]).then(values => { 
    console.log(values); 
    // [3, 1337, "foo"] 
});

如果iterable包含非promise值,则它们将被忽略,但仍然计入返回的promise数组值(如果promise已满足):

// this will be counted as if the iterable passed is empty, so it gets fulfilled
var p = Promise.all([1,2,3]);
// this will be counted as if the iterable passed contains only the resolved promise with value "444", so it gets fulfilled
var p2 = Promise.all([1,2,3, Promise.resolve(444)]);
// this will be counted as if the iterable passed contains only the rejected promise with value "555", so it gets rejected
var p3 = Promise.all([1,2,3, Promise.reject(555)]);

// using setTimeout we can execute code after the stack is empty
setTimeout(function(){
    console.log(p);
    console.log(p2);
    console.log(p3);
});

// logs
// Promise { <state>: "fulfilled", <value>: Array[3] }
// Promise { <state>: "fulfilled", <value>: Array[4] }
// Promise { <state>: "rejected", <reason>: 555 }

Asynchronicity or synchronicity of Promise.all

This following example demonstrates the asynchronicity (or synchronicity, if the iterable passed is empty) of Promise.all:

// we are passing as argument an array of promises that are already resolved,
// to trigger Promise.all as soon as possible
var resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)];

var p = Promise.all(resolvedPromisesArray);
// immediately logging the value of p
console.log(p);

// using setTimeout we can execute code after the stack is empty
setTimeout(function(){
    console.log('the stack is now empty');
    console.log(p);
});

// logs, in order:
// Promise { <state>: "pending" } 
// the stack is now empty
// Promise { <state>: "fulfilled", <value>: Array[2] }

The same thing happens if Promise.all rejects:

var mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)];
var p = Promise.all(mixedPromisesArray);
console.log(p);
setTimeout(function(){
    console.log('the stack is now empty');
    console.log(p);
});

// logs
// Promise { <state>: "pending" } 
// the stack is now empty
// Promise { <state>: "rejected", <reason>: 44 }

But, Promise.all resolves synchronously if and only if the iterable passed is empty:

var p = Promise.all([]); // will be immediately resolved
var p2 = Promise.all([1337, "hi"]); // non-promise values will be ignored, but the evaluation will be done asynchronously
console.log(p);
console.log(p2)
setTimeout(function(){
    console.log('the stack is now empty');
    console.log(p2);
});

// logs
// Promise { <state>: "fulfilled", <value>: Array[0] }
// Promise { <state>: "pending" }
// the stack is now empty
// Promise { <state>: "fulfilled", <value>: Array[2] }

Promise.all fail-fast behaviour

Promise.all is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then Promise.all will reject immediately.

var p1 = new Promise((resolve, reject) => { 
  setTimeout(resolve, 1000, 'one'); 
}); 
var p2 = new Promise((resolve, reject) => { 
  setTimeout(resolve, 2000, 'two'); 
});
var p3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 3000, 'three');
});
var p4 = new Promise((resolve, reject) => {
  setTimeout(resolve, 4000, 'four');
});
var p5 = new Promise((resolve, reject) => {
  reject('reject');
});

Promise.all([p1, p2, p3, p4, p5]).then(values => { 
  console.log(values);
}, reason => {
  console.log(reason)
});

//From console:
//"reject"

//You can also use .catch
Promise.all([p1, p2, p3, p4, p5]).then(values => { 
  console.log(values);
}).catch(reason => { 
  console.log(reason)
});

//From console: 
//"reject"

规范

规范 状态 备注
domenic/promises-unwrapping Standard Initial definition in an ECMA standard.
ECMAScript 2015 (6th Edition, ECMA-262)
Promise.all
Standard  

浏览器兼容性

No compatibility data found. Please contribute data for "javascript/promise" (depth: Promise.all) to the MDN compatibility data repository.

相关链接

文档标签和贡献者

 此页面的贡献者: higrw, xgqfrms-GitHub, rollinhup, Hushabyme, iugo, billcz, zilong-thu, fskuok
 最后编辑者: higrw,