Turn async functions into ES2015 generators
npx @tessl/cli install tessl/npm-babel--plugin-transform-async-to-generator@7.27.0@babel/plugin-transform-async-to-generator is a Babel plugin that transforms async functions into ES2015 generators, enabling async/await syntax to work in environments that don't natively support it. The plugin provides comprehensive transformation capabilities with configurable options for method wrapper functions and module imports.
npm install --save-dev @babel/plugin-transform-async-to-generator@babel/core ^7.0.0-0>=6.9.0import plugin from "@babel/plugin-transform-async-to-generator";For TypeScript types:
import type { Options } from "@babel/plugin-transform-async-to-generator";Note: This package is published as an ES module. For CommonJS environments, use dynamic import:
const plugin = (await import("@babel/plugin-transform-async-to-generator")).default;Use the plugin with Babel's built-in async-to-generator helper:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-transform-async-to-generator"]
};Use a custom async wrapper function from an external module:
// babel.config.js
module.exports = {
plugins: [
[
"@babel/plugin-transform-async-to-generator",
{
"module": "bluebird",
"method": "coroutine"
}
]
]
};Configure the plugin with Babel assumptions for optimized output:
// babel.config.js
module.exports = {
plugins: ["@babel/plugin-transform-async-to-generator"],
assumptions: {
"noNewArrows": false,
"ignoreFunctionLength": true
}
};The main export that creates a Babel plugin for transforming async functions to generators.
/**
* Default export: Babel plugin function that transforms async functions into ES2015 generators
* Created using @babel/helper-plugin-utils declare function
* @param api - Babel plugin API (from @babel/core)
* @param options - Plugin configuration options
* @returns Babel plugin object with visitor pattern
*/
const plugin: (api: object, options: Options) => {
name: string;
visitor: {
Function(path: object, state: object): void;
};
};
export default plugin;Transformation Behavior:
@babel/helper-remap-async-to-generator to perform the AST transformationawait expressions to yield expressions@babel/helper-module-importsasyncToGenerator helpernoNewArrows, ignoreFunctionLength) during transformationUsage Example:
// Input
async function fetchData() {
const response = await fetch('/api/data');
return await response.json();
}
// Output (default mode)
function fetchData() {
return _asyncToGenerator(function* () {
const response = yield fetch('/api/data');
return yield response.json();
})();
}
// Output (custom module mode with bluebird)
function fetchData() {
return _bluebird.coroutine(function* () {
const response = yield fetch('/api/data');
return yield response.json();
})();
}The plugin accepts an Options object to customize the async wrapper function.
Configuration Examples:
// Use bluebird coroutines
{
"method": "coroutine",
"module": "bluebird"
}
// Use custom async library
{
"method": "async",
"module": "my-async-lib"
}The plugin respects Babel assumptions for optimized output generation:
true): When true, avoids creating new arrow functions during transformation. Note: Babel 8 may change this default to falsefalse): When true, ignores the function.length property during transformationThese assumptions are passed to the internal remapAsyncToGenerator helper function which performs the actual AST transformation.
// babel.config.js with assumptions
module.exports = {
plugins: ["@babel/plugin-transform-async-to-generator"],
assumptions: {
"noNewArrows": false, // Allow new arrow functions
"ignoreFunctionLength": true // Ignore function.length
}
};Plugin configuration options interface (exported for TypeScript usage).
/**
* Configuration options for the async-to-generator transformation
*/
export interface Options {
/**
* Name of the wrapper method to import and use instead of built-in helper
* Must be used together with 'module' option
*/
method?: string;
/**
* Module name to import the wrapper method from
* Must be used together with 'method' option
*/
module?: string;
}The plugin works with standard Babel AST types from @babel/types and visitor patterns from @babel/traverse. The plugin's implementation uses these through the Babel plugin API but does not export them directly.
The plugin includes built-in error handling for common issues:
api.assertVersion(REQUIRED_VERSION(7))path.node.generator) or not async (!path.node.async)method and module options must be provided togetherThe plugin gracefully handles edge cases by returning early from the visitor when transformation is not applicable.
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
plugins: [
['@babel/plugin-transform-async-to-generator', {
module: 'bluebird',
method: 'coroutine'
}]
]
}
}
}
]
}
};import { transformSync } from '@babel/core';
import asyncToGeneratorPlugin from '@babel/plugin-transform-async-to-generator';
const result = transformSync(code, {
plugins: [
[asyncToGeneratorPlugin, {
module: 'bluebird',
method: 'coroutine'
}]
]
});