Last active
March 30, 2021 05:03
-
-
Save tonkotsuboy/5c1d96a35211f20b7d69cf93bc773d7d to your computer and use it in GitHub Desktop.
Top level await is already available in Node.js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this sample can be run with: $ node myTest.mjs | |
console.log("こんにちは"); | |
await new Promise(resolve => { | |
setTimeout(() => { | |
console.log("2秒経ちました"); | |
resolve(); | |
}, 2000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Top level await is already available in Node.js.
The await works without enclosing it within an async function.
There are two points to note:
・Can be used without flags from Node.js 14.8
・Set js to .mjs or specify type: "module" in package.json
I made a sample that can be run with:
https://twitter.com/tonkotsuboy_com/status/1376740922659008518