A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://babeljs.io/docs/plugins/transform-async-to-generator/ below:

@babel/plugin-transform-async-to-generator · Babel

@babel/plugin-transform-async-to-generator

info

This plugin is included in @babel/preset-env, in ES2017

In Babel 7, transform-async-to-module-method was merged into this plugin

Example

In

JavaScript

async function foo() {
await bar();
}

Out

JavaScript

var _asyncToGenerator = function (fn) {
...
};
var foo = _asyncToGenerator(function* () {
yield bar();
});

Out with options

Turn async functions into a Bluebird coroutine (caveats)

JavaScript

var Bluebird = require("bluebird");

var foo = Bluebird.coroutine(function*() {
yield bar();
});
Installation
npm install --save-dev @babel/plugin-transform-async-to-generator
yarn add --dev @babel/plugin-transform-async-to-generator
pnpm add --save-dev @babel/plugin-transform-async-to-generator
bun add --dev @babel/plugin-transform-async-to-generator
Usage With a configuration file (Recommended)

Without options:

babel.config.json

{
"plugins": ["@babel/plugin-transform-async-to-generator"]
}

With options:

babel.config.json

{
"plugins": [
[
"@babel/plugin-transform-async-to-generator",
{
"module": "bluebird",
"method": "coroutine"
}
]
]
}
Via CLI

Shell

babel --plugins @babel/plugin-transform-async-to-generator script.js
Via Node API

JavaScript

require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-async-to-generator"],
});
Caveats Bluebird non-promise runtime error

When using await with non-promise values, Bluebird will throw "Error: A value was yielded that could not be treated as a promise". Since Babel cannot automatically handle this runtime error, you should manually transform it to a promise.

async function foo() {
- await 42;
+ await Promise.resolve(42);
}
References

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4