• This function ensures that a provided promise is resolved within a specified time. If the promise does not resolve within the specified time, an error is thrown.

    Example


    import { timeout } from "@mrspartak/promises"
    import { api } from "./api"

    // Can be used as a race condition
    const [error, user] = await timeout(api.getUser(), 1000)
    if (error) {
    // error can be either a timeout error or an error from the api
    }

    Type Parameters

    • T

      The type of the value that the input promise resolves to.

    Parameters

    • promise: TimeoutIn<T>

      The promise to be awaited.

    • ms: number

      The number of milliseconds to wait.

    • Optional message: string

      The error message to throw.

    Returns Promise<TimeoutOut<T>>

    A promise that resolves to a tuple containing either an error or the result.