pAny
pAny
(predicate: AsyncPredicate) => async (collection: Collection) => boolean
One at a time, asynchronously test if any entry passes a condition
const gt2 = async n => n > 2 const anyGt2 = pAny(gt2) await anyGt2([1, 2]) // is false await anyGt2([1, 2, 3]) // is true await anyGt2({ a: 1, b: 2 }) // is false await anyGt2({ a: 1, b: 2, c: 3 }) // is true
const gt2 = async (n: number) => n > 2 const anyGt2 = pAny(gt2) await anyGt2([1, 2]) // is false await anyGt2([1, 2, 3]) // is true await anyGt2({ a: 1, b: 2 }) // is false await anyGt2({ a: 1, b: 2, c: 3 }) // is true
const gt2 = async n => n > 2 const anyGt2 = pAny(gt2) await anyGt2([1, 2]) // is false await anyGt2([1, 2, 3]) // is true await anyGt2({ a: 1, b: 2 }) // is false await anyGt2({ a: 1, b: 2, c: 3 }) // is true
const gt2 = async (n: number) => n > 2 const anyGt2 = pAny(gt2) await anyGt2([1, 2]) // is false await anyGt2([1, 2, 3]) // is true await anyGt2({ a: 1, b: 2 }) // is false await anyGt2({ a: 1, b: 2, c: 3 }) // is true
Sometimes we want to asynchronously test if any entry passes a condition. Below, we call a mock store API to see if any item in our shopping cart belongs to a bundle deal. If so, we should try to upsell the shopper.
const mockStoreApi = { hasBundleDeal: async item => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const anyItemHasDeal = pAny(mockStoreApi.hasBundleDeal) const shouldShowDeal = await anyItemHasDeal(shoppingCart) console.log('shouldShowDeal: ' + shouldShowDeal) // is true
const mockStoreApi = { hasBundleDeal: async (item: string) => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const anyItemHasDeal = pAny(mockStoreApi.hasBundleDeal)<string[]> const shouldShowDeal = await anyItemHasDeal(shoppingCart) console.log('shouldShowDeal: ' + shouldShowDeal) // is true
const mockStoreApi = { hasBundleDeal: async item => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const anyItemHasDeal = pAny( mockStoreApi.hasBundleDeal ) const shouldShowDeal = await anyItemHasDeal(shoppingCart) console.log('shouldShowDeal: ' + shouldShowDeal) // is true
const mockStoreApi = { hasBundleDeal: async (item: string) => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const anyItemHasDeal = pAny( mockStoreApi.hasBundleDeal )<string[]> const shouldShowDeal = await anyItemHasDeal(shoppingCart) console.log('shouldShowDeal: ' + shouldShowDeal) // is true
pAny awaits each item before moving onto the next. See
async.someLimit
if you want to run more than one call at a time.const start = new Date().getTime() const gt2 = async n => { const msPassed = Date.now() - start console.log(`ms passed: ${msPassed}, for n = ${n}`) await pWaitMs(20) return n > 2 } const anyGt2 = pAny(gt2) await anyGt2([2, 3, 4]) const totalMsPassed = Date.now() - start console.log(`total ms passed: ${totalMsPassed}`)
const start = new Date().getTime() const gt2 = async (n: number) => { const msPassed = Date.now() - start console.log(`ms passed: ${msPassed}, for n = ${n}`) await pWaitMs(20) return n > 2 } const anyGt2 = pAny(gt2)<number[]> await anyGt2([2, 3, 4]) const totalMsPassed = Date.now() - start console.log(`total ms passed: ${totalMsPassed}`)
const start = new Date().getTime() const gt2 = async n => { const msPassed = Date.now() - start console.log( `ms passed: ${msPassed}, for n = ${n}` ) await pWaitMs(20) return n > 2 } const anyGt2 = pAny(gt2) await anyGt2([2, 3, 4]) const totalMsPassed = Date.now() - start console.log(`total ms passed: ${totalMsPassed}`)
const start = new Date().getTime() const gt2 = async (n: number) => { const msPassed = Date.now() - start console.log( `ms passed: ${msPassed}, for n = ${n}` ) await pWaitMs(20) return n > 2 } const anyGt2 = pAny(gt2)<number[]> await anyGt2([2, 3, 4]) const totalMsPassed = Date.now() - start console.log(`total ms passed: ${totalMsPassed}`)