New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

async-generator

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-generator

A helper to create async generators

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
31
93.75%
Maintainers
1
Weekly downloads
 
Created
Source

Async Generator

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);
  }
}

Keywords

async

FAQs

Package last updated on 07 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts