Top-Level Await support in Node.js v14.3.0
The current version of Node.js added support for Top-Level Await just like Deno.js, and other languages like C#.
Now, It’s now possible to use the await keyword outside of async functions.
Let’s take a look at an example.
We will fetch a quote from API.
Installation:
Download and Install Node.js v14.3.0
Download | Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
nodejs.org
Setup:
Create Node.js project using :
npm init — yes
Now install the Axios library for fetching data from API.
npm install axios --save
Now modify apackage.json
file.
Take a look here, we added type as module
. Because the language has not fully implemented it yet stage 3.
Code goes like this,
Running this file using node --harmony-top-level-await top-level-async-await.js
.
Output:
Thank you.