Skip to main content

delay

TS JS Deno

Invokes the provided function after wait milliseconds.

Use setTimeout() to delay execution of fn. Use the spread (...) operator to supply the function with an arbitrary number of arguments.

typescript
const delay = (fn: Func, wait: number, ...args: any[]) =>
setTimeout(fn, wait, ...args);

// Return a promise, Resolve after `wait` milliseconds.
const delayedPromise = (wait: number = 300, ...args: any[]) =>
new Promise((resolve) => {
delay(resolve, wait, ...args);
});
typescript
delay(
function (text) {
console.log(text);
},
1000,
"later"
); // Logs 'later' after one second.

// delayedPromise
let counter = 0;
const updateState = () => {
counter++;
};
const debouncedUpdate = debounce(updateState);
debouncedUpdate(); // counter == 1
debouncedUpdate(); // counter == 1
await delayedPromise(); // counter == 1
assertEquals(counter, 1);