threadpool.js
A thread-pool abstraction for ES6 async operations
Install
npm install --save threadpool
Usage
Sort notation:
// Fetch the dev names of all GitHub projects,// executing no more than 10 active requests at any given timelet devNames = let range = ...Array100000 // (a neat ES6 trick for getting num ranges)await ThreadPool console
Full notation (allowes queueing, executing and waiting completion in different places):
// Fetch the metadata of all GitHub projects,// executing no more than 10 active requests at any given timelet devNames = // init the ThreadPool supplying it the max number of parallel executions.// an options object can also be supplied with a 'errorHandler' function,// an error thrown from this function will halt ThreadPool executionlet tp = 10 { throw err } for let i = 0; i < 10000000; i += 100 // queues sync / async functions or Promises (they don't start running yet) tp // starts execution (queueing new tasks still possible)tp // closes the ThreadPool for the queueing of new tasks (so it's completion can be awaited)tp // blocks untill all queued tasks completedtry await tp catcherr // an error from a task, rethrown by 'errorHandler' can be handled here console console