pAll

pAll

  • (predicate: AsyncPredicate) => async (collection: Collection) => boolean
  1. One at a time, asynchronously test if all entries pass a condition

    const gt2 = async n => n > 2
    const allGt2 = pAll(gt2)
    
    await allGt2([2, 3]) // is false
    await allGt2([3, 4]) // is true
    await allGt2({ b: 2, c: 3 }) // is false
    await allGt2({ c: 3, d: 4 }) // is true
    const gt2 = async (n: number) => n > 2
    const allGt2 = pAll(gt2)
    
    await allGt2([2, 3]) // is false
    await allGt2([3, 4]) // is true
    await allGt2({ b: 2, c: 3 }) // is false
    await allGt2({ c: 3, d: 4 }) // is true
    
    const gt2 = async n => n > 2
    const allGt2 = pAll(gt2)
    
    await allGt2([2, 3]) // is false
    await allGt2([3, 4]) // is true
    await allGt2({ b: 2, c: 3 }) // is false
    await allGt2({ c: 3, d: 4 }) // is true
    const gt2 = async (n: number) => n > 2
    const allGt2 = pAll(gt2)
    
    await allGt2([2, 3]) // is false
    await allGt2([3, 4]) // is true
    await allGt2({ b: 2, c: 3 }) // is false
    await allGt2({ c: 3, d: 4 }) // is true
    
  2. Sometimes we want to asynchronously test if all entries pass a condition. Below, we call a mock store API to see if all items in our shopping cart are in stock. If so, we should let them know of a pickup discount.

    const itemsInStock = new Set(['shoes', 'broom', 'mop', 'socks', 'detergent'])
    
    const storeApi = {
      inStock: async item => itemsInStock.has(item),
    }
    
    const allInStock = pAll(storeApi.inStock)
    const shoppingCart = ['broom', 'mop', 'detergent']
    const showPickupDiscount = await allInStock(shoppingCart)
    
    console.log(showPickupDiscount) // is true
    const itemsInStock = new Set(['shoes', 'broom', 'mop', 'socks', 'detergent'])
    
    const storeApi = {
      inStock: async (item: string) => itemsInStock.has(item),
    }
    
    const allInStock = pAll(storeApi.inStock)<string[]>
    const shoppingCart = ['broom', 'mop', 'detergent']
    const showPickupDiscount = await allInStock(shoppingCart)
    
    console.log(showPickupDiscount) // is true
    
    const itemsInStock = new Set([
      'shoes',
      'broom',
      'mop',
      'socks',
      'detergent',
    ])
    
    const storeApi = {
      inStock: async item => itemsInStock.has(item),
    }
    
    const allInStock = pAll(storeApi.inStock)
    const shoppingCart = ['broom', 'mop', 'detergent']
    const showPickupDiscount =
      await allInStock(shoppingCart)
    
    console.log(showPickupDiscount) // is true
    const itemsInStock = new Set([
      'shoes',
      'broom',
      'mop',
      'socks',
      'detergent',
    ])
    
    const storeApi = {
      inStock: async (item: string) =>
        itemsInStock.has(item),
    }
    
    const allInStock = pAll(storeApi.inStock)<
      string[]
    >
    const shoppingCart = ['broom', 'mop', 'detergent']
    const showPickupDiscount =
      await allInStock(shoppingCart)
    
    console.log(showPickupDiscount) // is true
    
  3. pAll awaits each item before moving onto the next. See async.everyLimit 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 allGt2 = pAll(gt2)
    
    await allGt2([3, 4, 5])
    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 allGt2 = pAll(gt2)<number[]>
    
    await allGt2([3, 4, 5])
    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 allGt2 = pAll(gt2)
    
    await allGt2([3, 4, 5])
    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 allGt2 = pAll(gt2)<number[]>
    
    await allGt2([3, 4, 5])
    const totalMsPassed = Date.now() - start
    console.log(`total ms passed: ${totalMsPassed}`)