我想澄清这一点,因为文件不是很清楚;

Q1: Promise.all(iterable)是按顺序还是并行处理所有的promise ?或者,更具体地说,它相当于运行链式承诺

p1.then(p2).then(p3).then(p4).then(p5)....

或者它是某种其他类型的算法,其中所有的p1, p2, p3, p4, p5等同时(并行)被调用,并且结果在所有解决(或一个拒绝)后立即返回?

问题2:如果承诺。所有的运行都是并行的,是否有一种方便的方法来运行一个可迭代对象?

注意:我不想使用Q或蓝鸟,而是所有本地ES6规格。


当前回答

使用async await可以很容易地按顺序执行promise数组:

let a = [promise1, promise2, promise3];

async function func() {
  for(let i=0; i<a.length; i++){
    await a[i]();
  }  
}

func();

注意:在上述实现中,如果一个承诺被拒绝,其余的承诺将不会被执行。如果你想要所有的承诺都被执行,那么将await a[i]();内部尝试捕获

其他回答

我一直在用for来求解连续的承诺。我不确定这是否有帮助,但这就是我一直在做的。

async function run() {
    for (let val of arr) {
        const res = await someQuery(val)
        console.log(val)
    }
}

run().then().catch()

是的,您可以按照如下方式链接一个promise返回函数数组 (这将每个函数的结果传递给下一个函数)。当然,您可以编辑它,将相同的参数(或不传递参数)传递给每个函数。

function tester1(a) { return new Promise(function(done) { setTimeout(function() { done(a + 1); }, 1000); }) } function tester2(a) { return new Promise(function(done) { setTimeout(function() { done(a * 5); }, 1000); }) } function promise_chain(args, list, results) { return new Promise(function(done, errs) { var fn = list.shift(); if (results === undefined) results = []; if (typeof fn === 'function') { fn(args).then(function(result) { results.push(result); console.log(result); promise_chain(result, list, results).then(done); }, errs); } else { done(results); } }); } promise_chain(0, [tester1, tester2, tester1, tester2, tester2]).then(console.log.bind(console), console.error.bind(console));

并行

await Promise.all(items.map(async (item) => { 
  await fetchItem(item) 
}))

优点:速度更快。所有的迭代都将启动,即使其中一个在以后失败。然而,它会“快速失败”。使用的承诺。allsettle,即使某些迭代失败,也要并行地完成所有迭代。从技术上讲,这些是并发调用,而不是并行调用。

按顺序

for (const item of items) {
  await fetchItem(item)
}

优点:循环中的变量可以在每次迭代中共享。行为类似于普通的命令式同步代码。

也可以使用递归函数用异步函数顺序处理可迭代对象。例如,给定一个数组a,使用异步函数someAsyncFunction()处理:

var a = [1, 2, 3, 4, 5, 6] function someAsyncFunction(n) { return new Promise((resolve, reject) => { setTimeout(() => { console.log("someAsyncFunction: ", n) resolve(n) }, Math.random() * 1500) }) } //You can run each array sequentially with: function sequential(arr, index = 0) { if (index >= arr.length) return Promise.resolve() return someAsyncFunction(arr[index]) .then(r => { console.log("got value: ", r) return sequential(arr, index + 1) }) } sequential(a).then(() => console.log("done"))

Bergi的回答帮助我使电话同步。我在下面添加了一个例子,我们在前面的函数被调用之后调用每个函数:

function func1 (param1) {
    console.log("function1 : " + param1);
}
function func2 () {
    console.log("function2");
}
function func3 (param2, param3) {
    console.log("function3 : " + param2 + ", " + param3);
}

function func4 (param4) {
    console.log("function4 : " + param4);
}
param4 = "Kate";

//adding 3 functions to array

a=[
    ()=>func1("Hi"),
    ()=>func2(),
    ()=>func3("Lindsay",param4)
  ];

//adding 4th function

a.push(()=>func4("dad"));

//below does func1().then(func2).then(func3).then(func4)

a.reduce((p, fn) => p.then(fn), Promise.resolve());