Socket
Book a DemoInstallSign in
Socket

babel-plugin-top-level-await

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-top-level-await

babel plugin for automatically wrapping top level awaits

0.7.1
latest
Source
npmnpm
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 10 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.