What is @babel/plugin-syntax-async-generators?
The @babel/plugin-syntax-async-generators package allows Babel to parse code that uses async generator functions. These are functions that can pause execution and resume asynchronously, making them useful for handling streams of data or other asynchronous operations in a more readable way than using Promises or callbacks alone.
What are @babel/plugin-syntax-async-generators's main functionalities?
Parsing async generator functions
This code defines an async generator function that yields numbers from 0 to 2. The @babel/plugin-syntax-async-generators package allows Babel to understand and parse this syntax.
async function* asyncGenerator() {
var i = 0;
while (i < 3) {
yield i++;
}
}
Other packages similar to @babel/plugin-syntax-async-generators
@babel/plugin-transform-async-to-generator
This package transforms async functions to generator functions. While @babel/plugin-syntax-async-generators only enables parsing of async generator syntax, @babel/plugin-transform-async-to-generator goes further by transforming async functions into a form that can be executed in environments that do not support async functions natively.
@babel/preset-env
A Babel preset that includes @babel/plugin-syntax-async-generators among many other plugins. It allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and thus plugins) are needed for your target environment. It compares to @babel/plugin-syntax-async-generators by providing a broader range of JavaScript feature support, including but not limited to async generators.
@babel/plugin-syntax-async-generators
Allow parsing of async generator functions.
Example
Syntax
async function* agf() {
await 1;
}
async function f() {
for await (let x of y) {
g(x);
}
}
Installation
npm install --save-dev @babel/plugin-syntax-async-generators
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["@babel/plugin-syntax-async-generators"]
}
Via CLI
babel --plugins @babel/plugin-syntax-async-generators script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-syntax-async-generators"]
});
References