2017-09-15 63 views
0

我在Windows上使用最新版本的NodeJS和正在此錯誤:/等待8.5

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條件之外,我仍然得到相同的錯誤。

回答

2

您不能在async功能之外有await功能。

你得到的錯誤是因爲這個。因此,將您的if包裝在async函數或其他東西中。

+0

即剛移動的誤差到下一個'await'指定: '如果(等待FNB()){' ^^^ ''' 語法錯誤:意外identifier' – erv

+0

我添加異步匿名回調函數並修正了這一點。乾杯! – erv