Resolve a collection of Promises while guarding against certain rejections
$ npm install --save promise-guard
promiseGuard(<promises>, <map>, <filter>)
Given an array or object of promises, attemps to resolve them to an array or object of the resolved values. Converts rejected promises for which filter(error)
is true, to resolved values of map(error)
. Returns a rejected promise if any rejection does not pass the filter.
<map>
defaults tofunction(x) { return x }
<filter>
defaults tofunction() { return true }
var promiseGuard = require('promise-guard')
// resolves to ['a', 'z']
promiseGuard(
[Promise.resolve('a'), Promise.reject('b')],
function(error, i) { return 'z' },
function(error, i) { return error === 'b' }
)
// rejects to 'c'
promiseGuard(
[Promise.resolve('a'), Promise.reject('b'), Promise.reject('c')],
function(error, i) { return 'z' },
function(error, i) { return error === 'b' }
)
// resolves to [1,2,3]
promiseGuard([Promise.resolve(1), Promise.resolve(2), Promise.reject(3)])
// resolves to { a:1, b:null }
promiseGuard(
{ a: Promise.resolve(1), b: Promise.reject('error') },
function(error, key) { return null }
)
ISC © Raine Lourie