Node.js 在node 8.5中使用async/await
我正在windows上使用最新版本的nodejs,出现以下错误:Node.js 在node 8.5中使用async/await,node.js,async-await,Node.js,Async Await,我正在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(functi
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条件之外,但仍然得到相同的错误。您不能将await
移到async
函数之外
你犯的错误就是因为这个。因此,请将
if
包装在异步
函数或其他东西中。您不能让等待
在异步
函数之外
你犯的错误就是因为这个。因此,将
if
封装在async
函数或其他东西中。将错误移动到下一个wait
名称:if(wait fnB()){
^^^`语法错误:意外标识符我在匿名回调函数中添加了async,并解决了这个问题。干杯!这只是将错误移到了下一个await
名称:if(await fnB()){
^^`语法错误:意外标识符我在匿名回调函数中添加了async,这就解决了这个问题。干杯!