stepp
v0.0.3
Published
Set a maximum concurrency for your Promises.
Downloads
5
Maintainers
Readme
stepp
Promise-compatiable Promise wrapper for controlling concurrency of promises.
Quick Start
The idea is for step to be a drop-in replacement for your promises (in terms of syntax),
but for the execution to be delayed. So you can create a step with a given job but it will
not execute until you run step.run()
. This can be used to "delay" execution (similar to
wrapping a promise with a function) or more importantly, used to limit concurrency.
To limit concurrency while executing many steps, you can do something like this:
const Step = require('stepp')
const values = [... new Array(1000).keys()]
const steps = values.map(id => {
return new Step((resolve, reject) => {
resolve(id)
})
})
// steps will be a list of steps where each step should resolve
// a different id from the list of values
//
// `.all()` returns a promise, so you can use it as you would use
// `Promise.all()`.
Step.all(steps)
.then(newValues => {
// newValues is deep equal to values
// not necessarily in the right order though - race conditions
// and what not
})
API
.all(steps, [options])
- steps (array of Steps): the steps to execute.
- options (object): configuration object with the following properties:
- failFast (boolean; default: true):
- if true, the all promise is rejected as soon as an error occurs.
- if false, the all promise is rejected after running all the steps with an array of errors.
- maxConcurrency (number): the maximum number of steps that can be run in parallel.
- failFast (boolean; default: true):
License
Licensed under MIT license.
Copyright © 2017-present Foko Inc. All rights reserved.