Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
lib
 
 
 
 
 
 
 
 

README.md

Top Level Await

This is a simple Babel plugin which automatically wraps top-level await expressions.

That is, it'll turn code like:

await fetch('http://www.google.com/')

into:

export const __async = true;
export default (async function(){
	await fetch('http://www.google.com/')
})()

This implementation automatically hoists import declarations so that they remain at the top level:

import _ from "lodash";
import fs from "fs-promise"

var text = await fs.readFile('README.md', 'utf-8')

console.log(text)

This is transpiled conveniently into:

import _ from "lodash";
import fs from "fs-promise"

export const __async = (async function(){
	var text = await fs.readFile('README.md', 'utf-8')

	console.log(text)
})()

In addition, transformed modules export __async which is the promise of the wrapped async expression.

The current implementation doesn't support exports. Top level awaits might interfere with the behavior of certain modules (as many of their exports may not be defined until well after the module has been loaded). Anyway, this top-level await transform is primarily useful for REPL-style interactions, as essentially a convenience to avoid wrapping things with an async IIFE (immediately invoked function expression).

Additional Information: https://github.com/tc39/ecmascript-asyncawait/issues/9

To Run:

There's a demo in the test/ folder

babel-node --presets es2015 transform.js

About

Babel plugin that automatically wraps top-level awaits

Resources

You can’t perform that action at this time.