unify usage of sleepAsync and add tests

The tests mock JS setTimeout API. However promise.resolve() is not working without flushing the promise queue (which could be done just by awaiting Promise.resolve()), similar issue has been discussed in facebook/jest#2157.
This commit is contained in:
undergroundwires
2021-05-04 19:10:10 +02:00
parent 49600c5f37
commit 36f0805590
4 changed files with 88 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
export type SchedulerType = (callback: (...args: any[]) => void, ms: number) => void;
export function sleepAsync(time: number, scheduler: SchedulerType = setTimeout) {
return new Promise((resolve) => scheduler(() => resolve(undefined), time));
}