README
random rejection
Return promises that may randomly return an error.
Usage
$ npm install --save random-rejection
const randomRejection = require('random-rejection')
API
Default
By default calling the function will just return a Promise
that will
instantly resolve or reject.
randomRejection()
.then(val => {
// if it resolves `val` will be "OK."
})
.catch(err => {
// otherwise you get a `RandomError`
})
Timed
A .timed([milliseconds])
method will use setTimeout
to simulate an async call that will either resolve or reject. Optionally you can specify
an amount of milliseconds that the Promise will wait to complete otherwise
the time will be random.
// after roughly 1000 ms resolve or reject
randomRejection.timed(1000)
.then(val => {
// if it resolves `val` will be "OK."
})
.catch(err => {
// otherwise you get a `RandomError`
})
All
Finally, a method is provided which will return an array of Promises each of which are timed randomly and any of them will potentially reject.
Promise.all(randomRejection.all())
.then(vals => {
// if it resolves `vals` will be an array of "OK."'s
})
.catch(err => {
// otherwise you get a `RandomError`
})