
How to use top-level await in Node.js
How to use top-level await in Node.js
This is a video about top-level await in Node.js. Top-level await is available in ECMAscript modules in Node.js. You can use it in two ways in your Node.js project. Use the mjs
file extension or enable it in your package.json
using \"type\": \"module\"
.
Blog post: https://www.stefanjudis.com/today-i-l…
Node.js release post: https://nodejs.org/en/blog/release/v1…
Say Hi on Twitter: https://twitter.com/stefanjudis
Content
0.535 -> Hey friends.
1.075 -> I just learned something cool
about Node.js and I thought
3.655 -> I'm going to share it with you.
5.125 -> If you were riding Node.js, you
might've come across situations in
8.155 -> which you're writing in Node.js script
to maybe batch rename some files or
12.655 -> do some image processing or fetch
some data from an API asynchronously.
18.265 -> Let's have a look at an example, and then
I'm going to show you what I learned,
20.965 -> what you see here is a quick and easy
project that uses the got npm package,
26.095 -> which you can use to do HTTP requests.
29.215 -> And then it's fetching some
data from the dog.ceo API, and
34.264 -> then it's logging this out.
35.494 -> So let's run this script
very, very quickly.
38.764 -> So you see here that, that it
returns a status and a message
41.674 -> and a random dog picture.
44.664 -> So, but now we have to 2021 and you
see here that I'm using the then
49.404 -> syntax when dealing with promises.
51.513 -> Usually these days, everybody
is all about async and await.
54.993 -> So how could you use async and await
in a function or in a script like this?
61.016 -> let's just refactor that for a moment.
62.786 -> So what we could do is we could do
data and we await the got function
68.206 -> with the json method, and then we're
gonna do console log and log out data.
75.726 -> So let's give that a try.
77.116 -> When I now run this.
78.541 -> You will see that Node.js is throwing
an error because await is only
82.501 -> valid in an proper async function.
85.561 -> What you had to do usually is to
write a function, let's call it main
90.921 -> and let's make that async and let's
move these kinds of things around to
95.001 -> have them in there and let's run main.
97.221 -> So let's give that another try
and let's see what happens.
101.391 -> Cool.
101.961 -> So we get, again, our asynchronous
promise-based HTTP response.
107.331 -> The problem here now is that we really,
we include now, a function that it's
111.681 -> only job is to enable us to use await
and today I learned that in Node.js since
119.056 -> version 14, there's actually top-level
await available, which means that we
124.776 -> can drop these lines and can make our
scripts a little bit more straightforward.
129.876 -> So let me show you how that works.
131.226 -> I just removed the async
function, wrapping everything.
134.616 -> So now when we run this script
again, you will see that, well, we
138.036 -> are still having the same error.
140.386 -> Obviously.
141.706 -> So the thing is that top-level
await in Node.js is only
145.456 -> available in ECMAScript modules.
148.336 -> There are two ways to do that.
149.786 -> What we can do is we can, first of all,
rename this file to use the file extension
154.386 -> mjs, which stands for module JavaScript.
158.236 -> So, if you're now dealing with
the JavaScript module, we have to
160.636 -> first, change our require statements.
163.766 -> These are not available in ECMAScript
modules, and we're changing
167.486 -> this to import got from "got".
170.306 -> And let's now give that a try.
173.696 -> And right.
175.736 -> It's not called index.js anymore.
177.926 -> So let's call that index.mjs and
you see there that top-level await.
183.326 -> So calling await without
having an asynchronous function
186.836 -> wrapper is now available.
188.426 -> And I think that is pretty, pretty sweet.
191.261 -> So, let me show you
another way to do that.
193.291 -> Let's rename this file back again
to JavaScript, and let's just
199.441 -> see if it still throws an error.
202.411 -> It does.
203.411 -> What we can do, additionally, is
we could also go if you're having a
207.251 -> project that includes a package.json,
and we can also go in here and we can
212.306 -> define a type of this whole package
or of this whole repository here.
217.556 -> And we can say "type": "module".
220.266 -> These are the two ways on how to
enable ECMAScript modules in Node.js.
224.666 -> So when we now run "node index.js", we
see that this file, our index.js file
231.356 -> is now treated as an ECMAScript module.
234.956 -> And I think this is pretty, pretty sweet.
237.566 -> If you want to learn more about that,
I wrote a quick blog post that you
241.806 -> can find in the description below.
244.116 -> YouTubers does have to do that.
245.886 -> And, let me know what you think
about that and see you next time.
Source: https://www.youtube.com/watch?v=UcrdtZwcsgY