我有一些使用 Async/Await 的代码并且工作正常......这是代码:
async function main() {
const x = await myfunction();
return x;
}
我想做这样的事情:
async function main2() {
const x = await myfunction();
const x2 = await myfunction2();
return x;
return x2;
}
我的问题是上面的代码不允许返回一个以上。
所以我的问题是...有没有办法让 main2() 上有多个返回值?
最佳答案
作为数组或对象返回
async function main2() {
const x = await myfunction();
const x2 = await myfunction2();
return [x, x2]
// or
return {f1: x, f2: x2}
}
main2()
.then(res => console.log(res))
.catch(err => console.log(err))
关于Javascript async/await 函数 - 添加多个返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56006931/