Skip to content

Helper functions to bring async support to ES6 generators today

License

Notifications You must be signed in to change notification settings

eggers/async-generator

Repository files navigation

Async Generator Build Status bitHound Overalll Score

Motivated by the Async Generator ES7 proposal https://github.com/jhusain/asyncgenerator

A couple helper functions to bring async support to ES6 generators today:

  • asyncIterator: Converts Promise<T[]> to Promise<T>[].
  • asyncPager: Creates a generator function will iterate through a paging function.
  • wrapObservable: Creates a generator function by wrapping an observable.
npm install async-generator

Examples

You'll need to include a polyfill for the regeneratorRuntime such as babel-polyfill

asyncIterator: Converts Promise<T[]> to Promise<T>[].

import {asyncIterator} from 'async-generator';

const items: Promise<T[]> = ...;

for (let promise of asyncIterator(items)) {
  const value: number = await promise;

  // value will be undefined if the array returned is empty or undefined.
  if (value !== undefined) {
    doSomething(value);
  }
}

asyncPager: Repeatedly calls a paging function.

import {asyncPager} from 'async-generator';

// data consumer
async function getPage(pageNumber: number) {
  // ...e.g. web request
  return null; // return null/undefined when there are no more pages.
}

for (let promise of asyncPager(getPage)) {
  const value: number = await promise;

  // value will be undefined if any page is empty.
  if (value !== undefined) {
    doSomething(value);
  }
}

wrapObservable: Wraps an Observable with an iterator

import {wrapObservable} from 'async-generator';

// data consumer
const observable = ...;

for (let promise of wrapObservable(observable)) {
  const value: number = await promise;

  // value will be undefined if any page is empty.
  if (value !== undefined) {
    doSomething(value);
  }
}

About

Helper functions to bring async support to ES6 generators today

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published