0.8.18 • Published 3 years ago

babel-plugin-transform-async-to-promises v0.8.18

Weekly downloads
164,248
License
MIT
Repository
github
Last release
3 years ago

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
@jvdx/babel@jvdx/babel-preset-jvdxtsdx-extraproject-name-herecloud-archive-s3@codewitchbella/microbundletakla-buildertakla-builder-presets@cyca/babel-presets@saaspe/componentsfe-cli-script@everything-registry/sub-chunk-1204p149-tabletemporaetwillow-build-toolswebchestep-interpreter@cpro-js/vite-ui5-integration-plugin@bung87/bilijm-rollupstyledbuttonxxsuntmolestiaesource-map-to-codetzcc-lg-v1ui5-tooling-transpiletranzpile@ambroseus/tsdx@aristidenf/streak-counter@anst/babel-config@akinoxsolutions/microbundle@alexandernanberg/bricks@backslashbuild/microbundle@bscswap/binance-connector@chialab/babel-preset@1stg/babel-preset@devorso/dcountdown@datadesk/baker@datagraphics/bakerzeusxtsdx-with-svgzero-bundlerzlc@ff0000-ad-tech/wp-deploy-manager@hasparus/microbundle@heatmanofurioso/microbundle@fishx/stark@eik/microbundle@graines-digitales/nice-handsome-button@infinitebrahmanuniverse/nolb-babel-plugin-t@knapsack/babel-config@lotus-ui/angela@leiyulf/gantt-task-react@inti-ar/evm-chains@iosio/rollup-plugin-custom-x-babel@lianfangti/gantt-task-react@m-fe/babel-preset@micro-web/microbundle@n3tr/tsdx@maujzs/branch-release@knapsack/babel-config-starter@jvdx/babel-preset@kay.eg/pkg@jarrku/foreach-tsdx@klutch-card/klutch-cli@kne/microbundle@kommunicate/kommunicate-chatbot-plugin@jsenv/js-module-fallback@jsenv/plugin-transpilation@jsenv/babel-config-map@jsenv/babel-plugin-description@jsenv/babel-plugin-map@jsenv/babel-plugins@jsenv/babel-preset@indicsoft/digit-ui-module-commonpt@nore/bundle-js@nore/plugin-js@react-vant/cli@qp-mongosh/async-rewriter2@quantfive/microbundle-crl@saasify/microbundle@probablyup/microbundle@projectbabelfish/defikingdoms-sdk@quercia/cli@nx-custom/rollup@paljs/create@pansy/cli@splish-me/microbundle@soyjak/utilsmuleifymicrobundlemicrobundle-alosdmicrobundle-cra-oprmicrobundle-crlmicrobundle-crl-postcss8microbundle-crl-runtimemicrobundle-crl-with-assetsmicrobundle-enhancedmina-scriptsp147-tablep148-table
0.8.18

3 years ago

0.8.17

3 years ago

0.8.16

4 years ago

0.8.15

6 years ago

0.8.14

6 years ago

0.8.13

6 years ago

0.8.12

6 years ago

0.8.11

6 years ago

0.8.10

6 years ago

0.8.9

6 years ago

0.8.8

6 years ago

0.8.7

6 years ago

0.8.6

6 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.7

7 years ago

0.5.6

7 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago