babel-plugin-transform-async-to-promises

Transform async/await to promise chains

MIT 36 个版本
安装
npm install babel-plugin-transform-async-to-promises
yarn add babel-plugin-transform-async-to-promises
pnpm add babel-plugin-transform-async-to-promises
bun add babel-plugin-transform-async-to-promises
README

babel-plugin-transform-async-to-promises

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

Input:

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

Output:

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

Output with hoist enabled:

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

Output with inlineHelpers enabled:

const fetchAsObjectURL = function(url) {
	try {
		return Promise.resolve(fetch(url)).then(function(response) {
			return Promise.resolve(response.blob()).then(URL.createObjectURL);
		});
	} catch(e) {
		return Promise.reject(e);
	}
}

Output with externalHelpers enabled:

In the normal case, helpers are added to the top of the file for the _async and _await functions (as well as others). This can cause bloat in a codebase due to duplication of helper code in every file. To avoid this, enable externalHelpers and those will be imported instead:

import { _async } from "babel-plugin-transform-async-to-promises/helpers";
import { _await } from "babel-plugin-transform-async-to-promises/helpers";

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

export default fetchAsObjectURL;

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)
  • Top level await support is experimental with compatible module bundler. Set topLevelAwait option to return when using SystemJS.

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
版本列表
0.8.18 2021-12-29
0.8.17 2021-12-20
0.8.16 2021-11-21
0.8.15 2019-10-26
0.8.14 2019-08-04
0.8.13 2019-07-20
0.8.12 2019-06-11
0.8.11 2019-05-26
0.8.10 2019-04-20
0.8.9 2019-04-08
0.8.8 2019-04-01
0.8.7 2019-03-24
0.8.6 2019-03-15
0.8.5 2019-02-24
0.8.4 2018-12-16
0.8.3 2018-12-11
0.8.2 2018-12-03
0.8.1 2018-11-25
0.8.0 2018-11-23
0.7.2 2018-11-18
0.7.1 2018-09-08
0.7.0 2018-06-15
0.6.1 2018-04-18
0.6.0 2018-04-17
0.5.7 2018-04-12
0.5.6 2018-04-02
0.5.5 2018-04-02
0.5.4 2018-03-18
0.5.3 2018-02-21
0.5.2 2018-02-20
0.5.1 2018-02-05
0.5.0 2018-01-27
0.4.0 2018-01-22
0.3.0 2018-01-10
0.2.0 2018-01-07
0.1.0 2018-01-06