Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Last active March 30, 2021 05:03
Show Gist options
  • Save tonkotsuboy/5c1d96a35211f20b7d69cf93bc773d7d to your computer and use it in GitHub Desktop.
Save tonkotsuboy/5c1d96a35211f20b7d69cf93bc773d7d to your computer and use it in GitHub Desktop.
Top level await is already available in Node.js.
// this sample can be run with: $ node myTest.mjs
console.log("こんにちは");
await new Promise(resolve => {
setTimeout(() => {
console.log("2秒経ちました");
resolve();
}, 2000);
});
@tonkotsuboy
Copy link
Author

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:

node myTest.mjs

https://twitter.com/tonkotsuboy_com/status/1376740922659008518

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment