<分区>
我正在寻找一种将多个 Promise 链接在一起的方法,类似于 Promise.all,但我希望每个 Promise 都等待最后一个。我确定这可能已经得到解答,但我想我不知道该用 google 搜索什么。
function output(msg) {
return new Promise((resolve,reject) => {
console.log(msg);
resolve();
});
}
output('1st').then(() => {
output('2nd').then(() => {
output('3rd').then(() => {
console.log('done')
});
});
});
// I want to be able to build from an array of length n
const msgs = ['1st','2nd','3rd'];
for(let i = 0; i < msgs.length;i++){
// Not sure what to do here
output(msgs[i]).then(() => {
output(msgs[i+1]);
});
}