What is @babel/plugin-transform-async-to-generator?
The @babel/plugin-transform-async-to-generator package is a Babel plugin that transforms async functions into generator functions using the regenerator runtime, which is a part of the Facebook Regenerator project. This transformation allows async functions to be used in environments that do not natively support async/await syntax by converting them into ES2015/ES6 compatible code.
Transform async functions to generators
This feature allows developers to write asynchronous code using the modern async/await syntax and have it transformed into generator functions that are compatible with older JavaScript engines.
async function foo() {
await bar();
}
// Transforms to:
function foo() {
return regeneratorRuntime.async(function foo$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return regeneratorRuntime.awrap(bar());
case 2:
case "end":
return _context.stop();
}
}
}, null, this);
}