{
PromiseTimeout,
TimeoutError,
}
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();
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)")
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)")