Skip to content

Latest commit

 

History

History
65 lines (53 loc) · 1.34 KB

API.md

File metadata and controls

65 lines (53 loc) · 1.34 KB

back to README.md

API Reference

Module exports

{
    PromiseTimeout,
    TimeoutError,
}

bindNative()

Attempts to define timeout on the native Promise properties. Returns the module exports so that this can be called on the same line as require.

Example

const { PromiseTimeout } = require('@whi/promise-timeout').bindNative();

new PromiseTimeout( executor, timeout = 1000, error_context )

Create a Promise that will trigger a timeout rejection if it is not settled within the timeout window.

Example usage

await new PromiseTimeout( (f,r) => {
    f( true );
}, 100 );
// true

await new PromiseTimeout( (f,r) => {
}, 100 );
// throw TimeoutError("Failed to settle promise within 0.1 second(s)")

Example usage with custom error context

await new PromiseTimeout( (f,r) => {
}, 100, "do something" );
// throw TimeoutError("Failed to do something within 0.1 second(s)")

Promise.timeout( promise, timeout, error_context )

Schedule a timeout trigger on an existing Promise object.

Example usage

await Promise.timeout(
    new Promise((f,r) => {
        f( true );
    }),
    100
);
// true

await Promise.timeout(
    new Promise((f,r) => {
    }),
    100
);
// throw TimeoutError("Failed to settle promise within 0.1 second(s)")