What is babel-plugin-transform-async-to-promises?
The babel-plugin-transform-async-to-promises package is a Babel plugin that transforms async/await syntax into Promises, allowing for more efficient and compatible asynchronous code execution in environments that do not support async/await natively.
What are babel-plugin-transform-async-to-promises's main functionalities?
Transform async functions to Promises
This feature allows you to write asynchronous code using async/await syntax, which the plugin then transforms into Promise-based code for better compatibility and performance.
const example = async () => {
const result = await someAsyncFunction();
return result;
};
Optimize async/await code
The plugin optimizes the transformed code to reduce the overhead associated with async/await, making the resulting Promise-based code more efficient.
const example = async () => {
const result1 = await someAsyncFunction1();
const result2 = await someAsyncFunction2();
return [result1, result2];
};
Other packages similar to babel-plugin-transform-async-to-promises
babel-plugin-transform-async-to-generator
This package transforms async/await syntax into generator functions. While it also aims to provide compatibility for environments that do not support async/await, it uses generator functions instead of Promises, which can result in different performance characteristics and code structure.
babel-plugin-syntax-async-functions
This package allows Babel to parse async functions but does not transform them. It is useful if you want to use async/await syntax in your code but rely on another tool or runtime to handle the actual transformation or execution.
babel-plugin-transform-regenerator
This package transforms generator functions and async functions to use regenerator runtime. It provides a broader transformation capability, including both async/await and generator functions, but may introduce additional runtime dependencies.
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 instrumentationnew AsyncFunction(...)
: impossible to support without shipping babel and the plugin in the output