2017-11-25 174 views
3

我試圖在我的項目中使用aysncawait我首先在FiddleJS中嘗試過這些,並且它工作正常,但是當我嘗試在我的IDE(PHPSTORM 2017)中執行時,我出現錯誤:異步和等待一些IDE的錯誤

async function test(url){ 
     ^^^^^^^^ 

SyntaxError: Unexpected token function 
    at createScript (vm.js:56:10) 
    at Object.runInThisContext (vm.js:97:10) 
    at Module._compile (module.js:542:28) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.runMain (module.js:604:10) 
    at run (bootstrap_node.js:389:7) 
    at startup (bootstrap_node.js:149:9) 

我也在vscode上試過並得到了同樣的錯誤。

這裏是我試圖做的:

async function test(url){ 
    console.log(await check(url)); 
} 

app.get('/test', (req, res) => { 

    var url = req.query.url; 
    console.log(url); 
    test(url); 

    console.log(url) 
}); 

function check(url){ 

    console.log(url); 

    return new Promise((resolve, reject) => { 

     request(url, function (error, response, body) { 

      if(response.headers.server === 'test'){ 
       url = url + '123'; 
       resolve(url); 
      } else { 
       reject('error'); 
      } 

     }); 

    }); 
} 

正如我所說的,在FiddleJS它工作正常(https://jsfiddle.net/voogcrbf/2/),不知道爲什麼會出現在IDE的錯誤。

希望你能幫助我。

回答

2

異步的await是在節點7.3版推出。什麼是您使用的node.js版本,它取決於那不ide。

+0

好點。錯誤本身就是在代碼執行過程中,與IDE無關,但是這段代碼中的語法高亮顯示錯誤是由於IDE沒有及時更新現代JS。 –

1

PhpStorm本身不支持最新的JavaScript.next。

我發現了用於IntelliJ產品的this JavaScript.next support plugin,這些產品可能會在某些功能進入核心之前有所使用。

從該網站的一些片段:

Javascript ES6/7 syntax improvements and additions.

Syntax highlights for annotations and async, await, from

+0

它不起作用。還有相同的錯誤 – zb22

+0

插件頁面上的某個人說你需要在''語言和框架' - >'javascript' - >'javascript語言版本'中設置語言版本,或許試試? –

+0

試過這個,我選擇了ECMAScript 6 – zb22