re-add and actually use promise timeout util
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
parent
bc90789c71
commit
5c24547ef5
2 changed files with 18 additions and 7 deletions
|
@ -22,6 +22,19 @@ import Promise from "bluebird";
|
|||
// Returns a promise which resolves with a given value after the given number of ms
|
||||
export const sleep = (ms: number, value: any): Promise => new Promise((resolve => { setTimeout(resolve, ms, value); }));
|
||||
|
||||
// Returns a promise which resolves when the input promise resolves with its value
|
||||
// or when the timeout of ms is reached with the value of given timeoutValue
|
||||
export async function timeout(promise: Promise, timeoutValue: any, ms: number): Promise {
|
||||
const timeoutPromise = new Promise((resolve) => {
|
||||
const timeoutId = setTimeout(resolve, ms, timeoutValue);
|
||||
promise.then(() => {
|
||||
clearTimeout(timeoutId);
|
||||
});
|
||||
});
|
||||
|
||||
return Promise.race([promise, timeoutPromise]);
|
||||
}
|
||||
|
||||
// Returns a Deferred
|
||||
export function defer(): {resolve: () => {}, reject: () => {}, promise: Promise} {
|
||||
let resolve;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue