async

Makes the function run after the given period of not being called. Useful to delay input submission for auto-complete etc.

Type signature

(f: F, wait: number) => (...args: unknown[]) => void

Examples

const f = () => console.log("Test");

const debounced = debounce(f, 2000);

debounced();
setTimeout(debounced, 1000);
setTimeout(debounced, 3000);
Try in REPL

Questions

  • How to make function fire after some time not being called?
  • How to debounce input events?
  • How to debounce a function?

TypeScript sourceJavaScript source

When awaited, delays the execution by the given number of milliseconds.

Type signature

(duration: number) => Promise<unknown>

Examples

delay(2000)(() =>
  console.log("Test"),
);
Try in REPL

Questions

  • How to delay a function?
  • What is the JavaScript version of sleep()?

TypeScript sourceJavaScript source

Runs the given tasks in a sequence.

Type signature

<T>(tasks: Task<T>[]) => Promise<Awaited<T>[]>

Examples

const f = () =>
  new Promise((resolve) =>
    setTimeout(resolve, 1000),
  );
const g = () =>
  new Promise((resolve) =>
    setTimeout(resolve, 2000),
  );

sequence([f, g]).then(() =>
  console.log("Done"),
);
Try in REPL

Questions

  • How to run async tasks sequentially?

TypeScript sourceJavaScript source