An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
Top Level Await in Node
Node v14.8.0 was released this week and with it came the unlocking of a commonly requested feature that I am excited about!
Node.js v14.8.0 has been released
Big highlight - Top Level Await has been unflagged!
HUGE thank you to @devsnek, @MylesBorins, & all others in the wide cast of characters who worked to bring this to you all!
For more information
nodejs.org/en/blog/releas16:56 PM - 11 Aug 2020
You may have run into the dreaded await is only valid in async function
syntax error when trying to write code like this:
const res = await fetch("https://dev.to");// SyntaxError: await is only valid in async function
As a workaround, it was common to see codebases use an immediately invoked function expression:
(async function() { const res = await fetch("https://dev.to");}());
Ugly - but functional! Alternative options include transpiling with Babel or using the command line flag --harmony-top-level-await
.
With v14.8.0, top level await has been unflagged and now just works. The only catch is that top level await is only supported in ES modules. This means either adding "type": "module"
to your package.json file or renaming your .js
file to .mjs
.
If your project can work with v14.8.0, you can take advantage of this feature today. For everyone else, you will still need to await a while.
Original Link: https://dev.to/mikeesto/top-level-await-in-node-2jad
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To