1
0
Fork 0
js-utils/src/Async.ts

47 lines
1.0 KiB
TypeScript
Raw Normal View History

import { TimeoutError } from './error/TimeoutError';
import { PredicateC0 } from './utils';
2020-03-29 13:43:52 +00:00
/**
* Resolve after a set amount of time.
* @public
2020-03-29 13:43:52 +00:00
*/
export function defer<T = undefined>(ms: number, val?: T): Promise<T> {
return new Promise((res, rej) => {
setTimeout(() => {
res(val);
}, ms);
});
}
/**
* Reject after a set amount of time if the original promise has not yet resolved.
* @public
2020-03-29 13:43:52 +00:00
*/
export function timeout<T>(ms: number, oper: Promise<T>): Promise<T> {
const limit = new Promise<T>((res, rej) => {
setTimeout(() => {
rej(new TimeoutError());
}, ms);
});
return Promise.race([limit, oper]);
}
/**
* Reject after a set number of attempts if the given predicate does not return true.
* @public
* @throws TimeoutError
*/
export async function waitFor(cb: PredicateC0, step: number, count: number): Promise<void> {
2020-03-29 13:43:52 +00:00
let accum = 0;
while (accum < count) {
await defer(step);
if (cb()) {
return;
}
accum += 1;
}
throw new TimeoutError();
}