1.0.0 • Published 6 years ago

babel-plugin-transform-async-await v1.0.0

Weekly downloads
14
License
MIT
Repository
github
Last release
6 years ago

babel-plugin-transform-async-await

FORK: this library if forked from babel-plugin-transform-async-to-promises, and modify _async/_await functions to be my own logic.

Babel plugin to transform async functions containing await expressions to the equivalent chain of Promise calls with use of minimal helper functions.

Build Status

Install

npm i -D babel-plugin-transform-async-await

Usage

Add this babel plugin into your config file .babelrc or config settings.

{
	"presets": [
    ["env", { "exclude": ["transform-async-to-generator", "transform-regenerator"] }],
		...
  ],
	...
	"plugins": [
		...
		"transform-async-await"
	]
}

Notice env preset's options, you should put exclude option to disable preset-env default plugins.

Input:

async function fetchAsObjectURL(url) {
	const response = await fetch(url);
	const blob = await response.blob();
	return URL.createObjectURL(blob);
}

Output:

var fetchAsObjectURL = _async(function(url) {
	return _await(fetch(url), function(response) {
		return _await(response.blob(), function(blob) {
			return URL.createObjectURL(blob);
		});
	});
});

Output with hoisting enabled:

var _URL$createObjectURL = function(blob) {
	return URL.createObjectURL(blob);
};
var _response$blob = function(response) {
	return _await(response.blob(), _URL$createObjectURL);
};
var fetchAsObjectURL = _async(function(url) {
	return _await(fetch(url), _response$blob);
});

JavaScript Language Features

Full Support

  • async/await
  • for/while/do loops (including loops that would exhaust stack if dispatched recursively)
  • switch statements (including fallthrough and default cases)
  • conditional expressions
  • logical expressions
  • try/catch
  • break/continue statements (on both loops and labeled statements)
  • throw expressions
  • Function hoisting
  • Variable hoisting
  • Arrow functions
  • Methods
  • arguments
  • this
  • Proper member dereference order of operations
  • Standards-compliant event loop scheduling

Partial Support

  • Function.length: async functions will often return a length of 0 (when the _async wrapper is used)

No support

  • eval: impossible to support without deep hooks into the runtime
  • Async generator functions: not implemented or planned
  • Function.name: rewrite pass removes function name instrumentation
  • new AsyncFunction(...): impossible to support without shipping babel and the plugin in the output