The timeout in milliseconds to wait before resolving or rejecting the promise.
Optional
resolveReject: boolean[Optional] If true the promise will resolve, otherwise it will reject.
Optional
message: T[Optional] The message to use when rejecting the promise, if not supplied (or undefined) the default message will be used.
A promise that will resolve or reject after the specified timeout.
// 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
});
Creates a Promise instance that resolve or reject after the specified timeout.