UNPKG

@fbltd/async

Version:

Miscellaneous async utils

25 lines (24 loc) 685 B
import { PromiseConfiguration } from "./promise-configuration.js"; export function delay(ms = 0, syncKey) { ms = Math.max(ms, 0); return delay.methods[typeof syncKey](ms); } Object.defineProperty(delay, 'methods', { value: { string: (ms) => { const start = performance.now(); while (true) { if (performance.now() - start >= ms) { return; } } }, undefined: (ms) => { const { promise, resolve } = new PromiseConfiguration(); setTimeout(() => { resolve(); }, ms); return promise; } } });