Skip to content

Latest commit

 

History

History
50 lines (43 loc) · 1.49 KB

delay.md

File metadata and controls

50 lines (43 loc) · 1.49 KB
title tags author_title author_url author_image_url description image
delay
function,intermediate
Deepak Vishwakarma
Implementation of "delay" in typescript, javascript and deno.

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.

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);
  });
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);