Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robertklep/top-level-await
Use `await` at your Node.js code's top level!
https://github.com/robertklep/top-level-await
async-await javascript nodejs
Last synced: 2 months ago
JSON representation
Use `await` at your Node.js code's top level!
- Host: GitHub
- URL: https://github.com/robertklep/top-level-await
- Owner: robertklep
- Created: 2017-05-23T09:13:18.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-05-24T08:12:20.000Z (over 7 years ago)
- Last Synced: 2024-10-07T08:46:39.644Z (3 months ago)
- Topics: async-await, javascript, nodejs
- Language: JavaScript
- Size: 2.93 KB
- Stars: 126
- Watchers: 2
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# top-level-await (PoC)
Allow using `await` at your code's top level. Requires Node.js v7.6.0 or higher.
Not this:
```
// 💩
(async function() {
console.log(await Promise.resolve('hello world'));
})();
```But this:
```
// 🎉
console.log(await Promise.resolve('hello world'));
```## Installation
```
npm i top-level-await
```## Usage
It's a two-step process: first `require()` this module, then `require()`
the rest of your code:```
// bootstrap.js
require('top-level-await');
require('./app');
```Inside `app.js`, you can use `await` whenever you like.
You can also tell Node to require the module for you, instead of using
a separate bootstrap script:
```
$ node -r top-level-await app
```Which works for CLI tools too:
```
#!/usr/bin/env node -r top-level-awaitconsole.log( await Promise.resolve('hello world') );
```## How?
By hacking `Module.wrap()`.
This is done without prejudice, so all modules that get loaded after loading this module will be "fixed".
## Is it production-ready?
```
¯\_(ツ)_/¯
```