Function timeout

  • Stop execution for a specified amount of time.

    Type Parameters

    • R = void

    Parameters

    • ms: number

      The time in milliseconds to wait before resolving the Promise.

    • Optional cb: (() => R)

      An optional callback function to execute before resolving the Promise.

        • (): R
        • Returns R

    Returns Promise<R>

    A Promise that resolves after the specified time has elapsed.

    Example

    Basic usage

    // using async/await
    async function main() {
    console.log('Before timeout.')
    await timeout(1000, () => console.log('A second has passed!'))
    console.log('After timeout.')
    }
    main()
    // Before timeout.
    // A second has passed!
    // After timeout.

    // using Promise.then()
    console.log('Before timeout.')
    timeout(
    1000,
    () => console.log('A second has passed!')
    ).then(
    () => console.log('Then...')
    )
    console.log('After timeout.')
    // Before timeout.
    // After timeout.
    // A second has passed!
    // Then...

Generated using TypeDoc