我需要对一组项目运行一些 promise ,但我不知道数组的长度。我如何按顺序运行这些 promise ?这就是我异步执行的方法:
const arrayABC = [a, b, c.....x, y, z] // length unknown
const promises = arrayABC.map(function(item) {
doSomething(item)
}
return Promise.all(promises)
我需要 promise 来一一落实。
最佳答案
假设我正确理解您的设置,这似乎是最合理的方法:
// return the promise chain from last element
return arrayABC.reduce(function (chain, item) {
// bind item to first argument of function handle, replace `null` context as necessary
return chain.then(doSomething.bind(null, item));
// start chain with promise of first item
}, doSomething(arrayABC.shift()));
关于javascript - 如何按顺序运行一系列 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42428187/