gpt4 book ai didi

javascript - 使用 Javascript/ECMAScript 6 等待多个 promise

转载 作者:行者123 更新时间:2023-11-29 10:12:50 25 4
gpt4 key购买 nike

我想开始一个 promises 列表并在所有这些都完成后执行回调(没有异步/等待)。

最佳答案

我刚刚想通了。只需使用 Promise.all:

function x(timeout) {
return new Promise((resolve, reject) => {
setTimeout(function() {
resolve(timeout + ' done!');
}, timeout);
});
}

(function() {
Promise.all([
x(300),
x(200),
x(100),
]).then(([x300, x200, x100]) => {
console.log(x100);
console.log(x200);
console.log(x300);
});
})();

关于javascript - 使用 Javascript/ECMAScript 6 等待多个 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29977262/

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