pWaitMs

pWaitMs

  • async (ms: number) => undefined
  1. Return a promise that resolves after ms

    const start = new Date()
    
    await pWaitMs(20)
    
    const end = new Date()
    const tookMs = end - start
    console.log(tookMs) // is close to 20
    const start = new Date().getTime()
    
    await pWaitMs(20)
    
    const end = new Date().getTime()
    const tookMs = end - start
    console.log(tookMs) // is close to 20
    
    const start = new Date()
    
    await pWaitMs(20)
    
    const end = new Date()
    const tookMs = end - start
    console.log(tookMs) // is close to 20
    const start = new Date().getTime()
    
    await pWaitMs(20)
    
    const end = new Date().getTime()
    const tookMs = end - start
    console.log(tookMs) // is close to 20
    
  2. We often need to asynchronously wait an amount of time. Below, we've had issues with our store API taking too long. Let's test it by mocking a timeout flow.

    const stock = new Set(['socks', 'football', 'pillows'])
    const storeApi = {
      isInStock: async item => {
        // let's time out half the time
        const durationMs = getRandomValue([10, 70])
        await pWaitMs(durationMs)
        return stock.has(item)
      },
    }
    
    const areSocksInStock = storeApi.isInStock('socks')
    const timeout = async () => {
      await pWaitMs(50)
      return 'call took longer than 50ms'
    }
    
    const result = await Promise.race([timeout(), areSocksInStock])
    console.log(result)
    // half the time this will show 'true'
    // the other half will show the timeout string
    const stock = new Set(['socks', 'football', 'pillows'])
    const storeApi = {
      isInStock: async (item: string) => {
        // let's time out half the time
        const durationMs = getRandomValue([10, 70])
        await pWaitMs(durationMs)
        return stock.has(item)
      },
    }
    
    const areSocksInStock = storeApi.isInStock('socks')
    const timeout = async () => {
      await pWaitMs(50)
      return 'call took longer than 50ms'
    }
    
    const result = await Promise.race([timeout(), areSocksInStock])
    console.log(result)
    // half the time this will show 'true'
    // the other half will show the timeout string
    
    const stock = new Set([
      'socks',
      'football',
      'pillows',
    ])
    const storeApi = {
      isInStock: async item => {
        // let's time out half the time
        const durationMs = getRandomValue([10, 70])
        await pWaitMs(durationMs)
        return stock.has(item)
      },
    }
    
    const areSocksInStock =
      storeApi.isInStock('socks')
    const timeout = async () => {
      await pWaitMs(50)
      return 'call took longer than 50ms'
    }
    
    const result = await Promise.race([
      timeout(),
      areSocksInStock,
    ])
    console.log(result)
    // half the time this will show 'true'
    // the other half will show the timeout string
    const stock = new Set([
      'socks',
      'football',
      'pillows',
    ])
    const storeApi = {
      isInStock: async (item: string) => {
        // let's time out half the time
        const durationMs = getRandomValue([10, 70])
        await pWaitMs(durationMs)
        return stock.has(item)
      },
    }
    
    const areSocksInStock =
      storeApi.isInStock('socks')
    const timeout = async () => {
      await pWaitMs(50)
      return 'call took longer than 50ms'
    }
    
    const result = await Promise.race([
      timeout(),
      areSocksInStock,
    ])
    console.log(result)
    // half the time this will show 'true'
    // the other half will show the timeout string