Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS script with async/await causing syntax error (v7.10.0)

I am trying to use async/await in NodeJS but my script is throwing a syntax error.

I was under the impression that async/await is supported naively since Node 7.6. When I run node -v I get v7.10.0.

Here is the contents of index.js:

async function getValueAsync() {
    return new Promise(function(resolve) {
        resolve('foo');
    });
}

let value = await getValueAsync();
console.log(value);

But when I invoke this script with node index.js I get:

let value = await getValueAsync();
                  ^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

I am running Linux Mint 18.1.

How can I get my script to compile and run?

like image 493
Jonathan.Brink Avatar asked May 21 '17 14:05

Jonathan.Brink


People also ask

How do you handle error in async await in node JS?

catch() is a method that returns a promise and its job is to deal with rejected promise. Now if we want to handle the Promise rejections using async/await then we can easily do it using a simple try/catch block as shown in the syntax given below.

Is await blocking Nodejs?

Though it creates a confusion, in reality async and await will not block the JavaScript main thread. Like mentioned above they are just syntactic sugars for promise chaining.

Can we use async await in node JS?

Async functions are available natively in Node and are denoted by the async keyword in their declaration. They always return a promise, even if you don't explicitly write them to do so. Also, the await keyword is only available inside async functions at the moment – it cannot be used in the global scope.

Does await stop code execution?

Promise creation starts the execution of asynchronous functionality. await only blocks the code execution within the async function. It only makes sure that the next line is executed when the promise resolves. So, if an asynchronous activity has already started, await will not have an effect on it.

What is async/await in Node JS?

With Node v8, the async/await feature was officially rolled out by the Node to deal with Promises and function chaining. The functions need not to be chained one after another, simply await the function that returns the Promise.

What happens if an async function returns an error?

} Remember that async functions always return promises. This promise rejects if any uncaught error occurs in the function. If your async function body returns a promise that rejects, the returned promise will reject too. run (). catch(function handleError(err) { err.message; // Oops!

Can you use await in a non-async function?

If we try to use await in a non-async function, there would be a syntax error: function f() { let promise = Promise.resolve(1); let result = await promise; // Syntax error } We may get this error if we forget to put async before a function. As stated earlier, await only works inside an async function.

How do you use async in JavaScript?

Let’s start with the async keyword. It can be placed before a function, like this: The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically.


1 Answers

await is only valid inside async functions, so you need, for example, an async IIFE to wrap your code with:

void async function() {
  let value = await getValueAsync();
  console.log(value);
}();

And, since return values from async functions are wrapped by a promise, you can shorten getValueAsync to simply this:

async function getValueAsync() {
  return 'foo';
}

Or don't mark it as async and return a promise from it:

function getValueAsync() {
  return new Promise(function(resolve) {
    resolve('foo');
  });
}
like image 159
robertklep Avatar answered Oct 08 '22 18:10

robertklep