我試圖在我的項目中使用aysnc
和await
我首先在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的錯誤。
希望你能幫助我。
好點。錯誤本身就是在代碼執行過程中,與IDE無關,但是這段代碼中的語法高亮顯示錯誤是由於IDE沒有及時更新現代JS。 –