Code Monkey home page Code Monkey logo

batch-promises's Introduction

batch-promises

Easily batch promises.

NOTE: Requires Promsies to be supported by runtime or polyfilled.

API

default export

type Promisable<T> = T | Promise<T>;

type Iterator<T, U> = (item: T) => Promisable<U>;

declare function batchPromises<T, U>(
  batchSize: number,
  collection: Promisable<T[]>,
  callback: Iterator<T, U>
): Promise<U[]>;

export = batchPromises;

callback will be called for each item in collection, parallel in batches of size batchSize, which are run in order once each previous batch is resolved.

To better illustrate, with a collection of 5 items ([1, 2, 3, 4, 5]) , and a batch size of 2:

  • Call callback for item 1 and 2 and wait for both promises to resolve, THEN
  • Call callback for item 3 and 4 and wait for both promises to resolve, THEN
  • Call callback for item 5 and wait for promise to resolve, THEN
  • Return an ordered array with the result of each callback

Use

import batchPromises from 'batch-promises';

batchPromises(
  2,
  [1, 2, 3, 4, 5],
  (i) =>
    new Promise((resolve, reject) => {
      // The iteratee will fire after each batch resulting in the following behaviour:
      // @ 100ms resolve items 1 and 2 (first batch of 2)
      // @ 200ms resolve items 3 and 4 (second batch of 2)
      // @ 300ms resolve remaining item 5 (last remaining batch)
      setTimeout(() => {
        resolve(i);
      }, 100);
    })
).then((results) => {
  console.log(results); // [1, 2, 3, 4, 5]
});

batch-promises's People

Contributors

a-ogilvie avatar nfroidure avatar nicktho avatar nwinch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.