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?
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.
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.
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.
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.
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.
} 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!
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.
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.
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');
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With