Function createTimeoutPromise

  • Creates a Promise instance that resolve or reject after the specified timeout.

    Type Parameters

    • T = any

    Parameters

    • timeout: number

      The timeout in milliseconds to wait before resolving or rejecting the promise.

    • OptionalresolveReject: boolean

      [Optional] If true the promise will resolve, otherwise it will reject.

    • Optionalmessage: T

      [Optional] The message to use when rejecting the promise, if not supplied (or undefined) the default message will be used.

    Returns IPromise<T>

    A promise that will resolve or reject after the specified timeout.

    0.5.0

    // Rejects after 100ms with default message
    const result = await createTimeoutPromise(100);
    // Throws an Error: Timeout of 100ms exceeded

    // Resolves after 100ms with default message
    const result = await createTimeoutPromise(100, true);
    console.log(result); // Timeout of 100ms exceeded

    // Rejects after 100ms with default message
    const result = await createTimeoutPromise(100, false);
    // throws an Error: Timeout of 100ms exceeded

    // Resolves after 100ms with default message
    const result = await createTimeoutPromise(100, true);
    console.log(result); // Timeout of 100ms exceeded

    // Rejects after 100ms with the message "Hello"
    const result = await createTimeoutPromise(100, false, "Hello");
    // throws an Error: Hello

    // Resolves after 100ms with the message "Hello"
    const result = await createTimeoutPromise(100, true, "Hello");
    console.log(result); // Hello

    // Resolves after 100ms with the message "Hello"
    doAwait(createTimeoutPromise(100, true, "Hello"), (result) => {
    console.log(result); // Hello
    });

    // Rejects after 100ms with the message "Hello"
    doAwait(createTimeoutPromise(100, false, "Hello"), (result) => {
    // Not called
    }, (err) => {
    console.log(err); // Hello
    });

    // Rejects after 100ms with the message "Hello"
    doAwaitResult(createTimeoutPromise(100, false, "Hello"), (result) => {
    console.log(result.rejected); // true
    console.log(result.reason); // Hello
    });