1. babel-plugin-transform-async-generator-functions
Turn async generator functions into ES2015 generators
babel-plugin-transform-async-generator-functions
Package: babel-plugin-transform-async-generator-functions
Created by: babel
Last modified: Mon, 13 Jun 2022 04:04:01 GMT
Version: 6.24.1
License: MIT
Downloads: 1,976,827
Repository: https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-async-generator-functions

Install

npm install babel-plugin-transform-async-generator-functions
yarn add babel-plugin-transform-async-generator-functions

babel-plugin-transform-async-generator-functions

Turn async generator functions and for-await statements to ES2015 generators

Example

In

 async function* agf() {
  await 1;
  yield 2;
}

Out

 var _asyncGenerator = ...

let agf = (() => {
  var _ref = _asyncGenerator.wrap(function* () {
    yield _asyncGenerator.await(1);
    yield 2;
  });

  return function agf() {
    return _ref.apply(this, arguments);
  };
})();

For await example

 async function f() {
  for await (let x of y) {
    g(x);
  }
}

Example Usage

 async function* genAnswers() {
  var stream = [ Promise.resolve(4), Promise.resolve(9), Promise.resolve(12) ];
  var total = 0;
  for await (let val of stream) {
    total += await val;
    yield total;
  }
}

function forEach(ai, fn) {
  return ai.next().then(function (r) {
    if (!r.done) {
      fn(r);
      return forEach(ai, fn);
    }
  });
}

var output = 0;
forEach(genAnswers(), function(val) { output += val.value })
.then(function () {
  console.log(output); // 42
});

Try it Out in the REPL

Installation

 npm install --save-dev babel-plugin-transform-async-generator-functions

Usage

Via .babelrc (Recommended)

.babelrc

 {
  "plugins": ["transform-async-generator-functions"]
}

Via CLI

 babel --plugins transform-async-generator-functions script.js

Via Node API

 require("babel-core").transform("code", {
  plugins: ["transform-async-generator-functions"]
});

References

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin