这篇翻译不完整。请帮忙从英语翻译这篇文章

概述

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 }

Promise.all的同步和异步

下面的示例展示了Promise.all的异步(当传递的可迭代对象是空时,是同步的):

// 我们为了尽快地触发 Promise.all 
// 传递了已经被resolve的promise数组作为参数
var resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)];

var p = Promise.all(resolvedPromisesArray);
// 立刻打印p的值
console.log(p);

// 使用setTimeout我们可以在stack为空时执行代码
setTimeout(function(){
    console.log('the stack is now empty');
    console.log(p);
});

// 按顺序打印:
// Promise { <state>: "pending" } 
// 现在stack是空的
// Promise { <state>: "fulfilled", <value>: Array[2] }

如果Promise.all reject时会出现同样的结果:

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);
});

// 打印
// Promise { <state>: "pending" } 
// 现在stack是空的
// Promise { <state>: "rejected", <reason>: 44 }

但是,当且只当传递的可迭代对象为空时,Promise.all会变成同步执行 

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);
});

// 打印
// Promise { <state>: "fulfilled", <value>: Array[0] }
// Promise { <state>: "pending" }
// 现在stack是空的
// Promise { <state>: "fulfilled", <value>: Array[2] }

Promise.all 快速失败行为

如果任意一个元素是rejected,那么Promise.all就是rejected。例如,如果你传递了五个参数,其中四个是经过一定时间间隔后执行resolve,另外一个立刻执行reject,那么Promise.all将会立刻执行reject。

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.

相关链接

文档标签和贡献者

 最后编辑者: BearZ,