gpt4 book ai didi

javascript - Promise.all 内的函数调用

转载 作者:行者123 更新时间:2023-12-01 00:37:32 24 4
gpt4 key购买 nike

我在 Promise.all 中传递了一个函数(返回一个 promise ),但没有得到预期的输出

function first() {
Promise.all([fun, fun1, fun2])
.then(function(values) {
console.log(values);
})
.catch(e => console.log(e));
}

function fun() {
return new Promise((res, re) => {
res(console.log("dd"));
});
}

function fun1() {
return Promise.resolve(3);
}

function fun2() {
return 2;
}

//calling function here
first();

预期:[dd,3,2]

实际:[ [函数:fun],[函数:fun1],[函数:fun2]]

最佳答案

I have passed a function (which returns a promise) inside Promise.all

不正确。您应该传递从该函数返回的 promise 。因此,如果 fun 返回一个 promise ,而不是

Promise.all([fun, fun1, fun2])

你应该这样做

Promise.all([fun(), fun1(), fun2()])

关于javascript - Promise.all 内的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57981712/

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