pDiscardWhen
pDiscardWhen
(predicate: AsyncPredicate) => async (collection: Collection) => Collection
Discard entries that pass an asynchronous condition.
The predicate is called for all entries at once. See Notes for an example.
const gt2 = async n => n > 2 const discardWhenGt2 = pDiscardWhen(gt2) const arr = [4, 1, 3, 2] await discardWhenGt2(arr) // is [1, 2] const obj = { d: 4, a: 1, c: 3, b: 2 } await discardWhenGt2(obj) // is { a: 1, b: 2 }
const gt2 = async (n: number) => n > 2 const discardWhenGt2 = pDiscardWhen(gt2) const arr = [4, 1, 3, 2] await discardWhenGt2(arr) // is [1, 2] const obj = { d: 4, a: 1, c: 3, b: 2 } await discardWhenGt2(obj) // is { a: 1, b: 2 }
const gt2 = async n => n > 2 const discardWhenGt2 = pDiscardWhen(gt2) const arr = [4, 1, 3, 2] await discardWhenGt2(arr) // is [1, 2] const obj = { d: 4, a: 1, c: 3, b: 2 } await discardWhenGt2(obj) // is { a: 1, b: 2 }
const gt2 = async (n: number) => n > 2 const discardWhenGt2 = pDiscardWhen(gt2) const arr = [4, 1, 3, 2] await discardWhenGt2(arr) // is [1, 2] const obj = { d: 4, a: 1, c: 3, b: 2 } await discardWhenGt2(obj) // is { a: 1, b: 2 }
Sometimes we want to discard entries that pass an asynchronous condition. Below, we need to find the items in our shopping cart requiring a shipping fee. Let's call the mock store API to remove items in our shopping cart with free shipping.
const mockStoreApi = { hasFreeShipping: async item => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const discardFreeShippingItems = pDiscardWhen(mockStoreApi.hasFreeShipping) const itemsWithShippingCharge = await discardFreeShippingItems(shoppingCart) console.log(itemsWithShippingCharge) // is ['broom', 'mop']
const mockStoreApi = { hasFreeShipping: async (item: string) => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const discardFreeShippingItems = pDiscardWhen(mockStoreApi.hasFreeShipping)< string[] > const itemsWithShippingCharge = await discardFreeShippingItems(shoppingCart) console.log(itemsWithShippingCharge) // is ['broom', 'mop']
const mockStoreApi = { hasFreeShipping: async item => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const discardFreeShippingItems = pDiscardWhen( mockStoreApi.hasFreeShipping ) const itemsWithShippingCharge = await discardFreeShippingItems(shoppingCart) console.log(itemsWithShippingCharge) // is ['broom', 'mop']
const mockStoreApi = { hasFreeShipping: async (item: string) => item === 'detergent', } const shoppingCart = ['broom', 'mop', 'detergent'] const discardFreeShippingItems = pDiscardWhen( mockStoreApi.hasFreeShipping )<string[]> const itemsWithShippingCharge = await discardFreeShippingItems(shoppingCart) console.log(itemsWithShippingCharge) // is ['broom', 'mop']
pDiscardWhen calls the predicate for all entries at once. See
async.filterLimit
if you want to limit the concurrency.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 discardWhenGt2 = pDiscardWhen(gt2) await discardWhenGt2([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 discardWhenGt2 = pDiscardWhen(gt2)<number[]> await discardWhenGt2([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 discardWhenGt2 = pDiscardWhen(gt2) await discardWhenGt2([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 discardWhenGt2 = pDiscardWhen(gt2)<number[]> await discardWhenGt2([2, 3, 4]) const totalMsPassed = Date.now() - start console.log(`total ms passed: ${totalMsPassed}`)