gpt4 book ai didi

node.js - 在 Node 8.5 中使用 async/await

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:31 25 4
gpt4 key购买 nike

我在 Windows 上使用最新版本的 Nodejs,但收到此错误:

if(await fnA()) {
^^^

SyntaxError: Unexpected identifier

这是我的代码:

if(await fnA()) {
console.log("1");
} else {
console.log("error at 1: ");
}

async function fnA() {
return new Promise(function (resolve, reject) {
if(await fnB()) {
console.log("A");
resolve(true);
} else {
console.log("error at A: ");
reject();
}
});
}

function fnB() {
return new Promise(function (resolve, reject) {
console.log("fnB");
setTimeout(function() {
console.log("fnB after delay");
resolve(true);
}, 3000);
});
}

我尝试将 await 移到 if 条件之外,但仍然遇到相同的错误。

最佳答案

目前还不能在 async 函数之外使用 await

你得到的错误就是因为这个。因此,将您的 if 包装在 async 函数或其他东西中。

关于node.js - 在 Node 8.5 中使用 async/await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46230788/

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