async-generator

async-generator is a lightweight shim for async generator functions. - https://github.com/rafaelgieschke/async-generator

Usage

It allows you to write:

async function* getFiles() {
  for (var i = 0; i < 10; i++) {
    yield (await fetch(i)).text();
  }
  return "done";
}

as:

import asyncGenerator from "https://rafaelgieschke.github.io/async-generator/async-generator.js";
const getFiles = asyncGenerator(function* () {
  for (var i = 0; i < 10; i++) {
    yield [(yield fetch(i)).text()];
  }
  return "done";
});

(You can also use include <script src="https://rafaelgieschke.github.io/async-generator/async-generator.global.js"></script> to have ayncGenerator defined in the global scope without ECMAScript modules.)

So, you simply have to replace await with yield and yield ... with yield [...].

(This also means, you cannot “await” an Array.)

Async iteration

You do not need a shim for async iteration, as you can always write:

(async () => {
  for await (const file of getFiles()) {
    console.log(file);
  }
})();

as:

(async () => {
  for (let file, __iter = getFiles()[Symbol.asyncIterator](); !({value: file} = await __iter.next()).done;) {
    console.log(file);
  }
})();

License

MIT


Fork me on GitHub