gpt4 book ai didi

javascript - 然后使用数组链接

转载 作者:行者123 更新时间:2023-11-30 06:21:02 25 4
gpt4 key购买 nike

<分区>

我正在寻找一种将多个 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]);
});
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com