Skip to content

Library to execute promises in parallel, keeping not to execute more than N promises at any one time

Notifications You must be signed in to change notification settings

eetay/promise-pool-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny Promise Pool

A tiny library to execute multiple promises in parallel, keeping not to execute more than N promises at any one time, using a promise.

  • Tiny Promise Pool is reviewed by Codacy Badge
  • Tiny promise Pool is unit tested with Jest

Install:

npm i tiny-promise-pool

Use with creator function:

var promisePool = require('promise-pool')

function nextPromise({index, data}) {
  if (index>=20) return null // no more
  return new Promise(function(res, rej) {
    res(index*2 + data)
  })
}

var all = promisePool({
  threads: 3,                 // maximum parallel promises
  next_promise: nextPromise,  // function to get/generate next promise
  next_promise_data: 17       // user data for the next_promise function
})

all.then(function(result) {
  console.dir(result)         // after all promises are resolved
})

Result:

[
  { context: { index: 0, thread: 0, data: 17 }, promise: Promise { 17 }, result: 17 },
  { context: { index: 1, thread: 1, data: 17 }, promise: Promise { 19 }, result: 19 },
  { context: { index: 2, thread: 2, data: 17 }, promise: Promise { 21 }, result: 21 },
  { context: { index: 3, thread: 0, data: 17 }, promise: Promise { 23 }, result: 23 },
  { context: { index: 4, thread: 1, data: 17 }, promise: Promise { 25 }, result: 25 },
  { context: { index: 5, thread: 2, data: 17 }, promise: Promise { 27 }, result: 27 },
  { context: { index: 6, thread: 0, data: 17 }, promise: Promise { 29 }, result: 29 },
  { context: { index: 7, thread: 1, data: 17 }, promise: Promise { 31 }, result: 31 },
  { context: { index: 8, thread: 2, data: 17 }, promise: Promise { 33 }, result: 33 },
  { context: { index: 9, thread: 0, data: 17 }, promise: Promise { 35 }, result: 35 },
  { context: { index: 10, thread: 1, data: 17 }, promise: Promise { 37 }, result: 37 },
  { context: { index: 11, thread: 2, data: 17 }, promise: Promise { 39 }, result: 39 },
  { context: { index: 12, thread: 0, data: 17 }, promise: Promise { 41 }, result: 41 },
  { context: { index: 13, thread: 1, data: 17 }, promise: Promise { 43 }, result: 43 },
  { context: { index: 14, thread: 2, data: 17 }, promise: Promise { 45 }, result: 45 },
  { context: { index: 15, thread: 0, data: 17 }, promise: Promise { 47 }, result: 47 },
  { context: { index: 16, thread: 1, data: 17 }, promise: Promise { 49 }, result: 49 },
  { context: { index: 17, thread: 2, data: 17 }, promise: Promise { 51 }, result: 51 },
  { context: { index: 18, thread: 0, data: 17 }, promise: Promise { 53 }, result: 53 },
  { context: { index: 19, thread: 1, data: 17 }, promise: Promise { 55 }, result: 55 }
]

Use with array of promises:

function makePromise(i) {
  return new Promise(function (resolve, _reject) {
    resolve(i)
  })
}

const promiseList = [
  makePromise(0),
  makePromise(1)
]

promisePool({
  threads: 3,
  next_promise: promiseList,              // List of promises
  next_promise_data: 'data for context'
}).then(function(result) {
  ...
})

Nested promise pools:

const innerPool = promisePool({
  threads: 2,
  next_promise: [
    makePromise(2),
    makePromise(3),
    makePromise(4),
    makePromise(5)
  ],
  next_promise_data: 'secondary promise pool'
})
const pool = promisePool({
  threads: 3,
  next_promise: [
    makePromise(0),
    makePromise(1),
    innerPool
  ],
  next_promise_data: 'primary promise pool'
})
pool.then(function(result) {
  ...
}

About

Library to execute promises in parallel, keeping not to execute more than N promises at any one time

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published