我正在使用Promise.all(tasks)
来跟踪我执行的任务的整体完成情况(这是我的主要目标)。我还想跟踪个别任务的完成情况,我该怎么做?
答案 0 :(得分:1)
没有什么可以阻止你连接个别处理程序以及等待它们:
let tasks = …;
for ([t, i] of tasks.entries())
t.then(res => {
console.log("task "+i+" completed with", res);
}, err => {
console.log("task "+i+" failed because", err);
});
Promise.all(tasks).then(all => {
console.log("all tasks completed");
}, err => {
console.log("something failed");
});
答案 1 :(得分:1)
尝试使用Array.prototype.map()
将承诺传递给Promise.all()
var promises = [Promise.resolve("a"), Promise.resolve("b")];
Promise.all(promises.map(function(p, index) {
return p.then(function(data) {
console.log("inside .map()", data, "index", index)
return data
}, function(err) {
console.log(err);
return err
})
}))
.then(function(complete) {
console.log("all promises after .map()", complete)
}, function(err) {
console.log("err", err)
})