shisama
11/9/2017 - 2:32 PM

babel-plugin-transform-async-generator-functions sample

babel-plugin-transform-async-generator-functions sample

{
  "name": "async-generator-functions-sample",
  "version": "1.0.0",
  "description": "async generators / iterators sample with babel",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "babel src -d dist",
    "start": "babel-node dist/async.js"
  },
  "author": "Masashi Hirano (https://github.com/shisama)",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-plugin-transform-async-generator-functions": "^6.24.1",
    "babel-preset-env": "^1.6.1"
  },
  "babel": {
    "presets": [
      [
        "env",
        {
          "targets": {
            "node": 8
          },
          "excludes": ["transform-regenerator"]
        }
      ]
    ],
    "plugins": [
      "transform-async-generator-functions"
    ]
  }
}
async function* foo() {
  const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];

  for await (const promise of promises) {
    yield promise;
  }
}

async function bar() {
  const f = foo();
  while (true) {
    const next = await f.next();
    if (next.done) {
      break;
    }
    console.log(next.value);
  }
}

bar();