Async and await error with some IDE's

Viewed 699

I'm trying to use aysnc and await in my project, I first tried these in FiddleJS, and it works fine but when I tried to do it in my IDE (PHPSTORM 2017) I got an error:

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)

I also tried on vscode and got the same error.

Here is what I tried to do:

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');
            }

        });

    });
}

As I said, in FiddleJS it working fine (https://jsfiddle.net/voogcrbf/2/), don't know why there is an error in the IDE's.

Hope you could help me with that.

2 Answers

Async await was introduced in node v7.3. What is the node.js version you are using, it depends on that not the ide.

PhpStorm doesn't natively support the latest JavaScript.next yet.

I found this JavaScript.next support plugin for IntelliJ products that may be some use until they get the functionality into core.

Some snippets from that site:

Javascript ES6/7 syntax improvements and additions.

Syntax highlights for annotations and async, await, from

Related