Snippets

Delay

TypeScript Promise Delay

Wait for a period of time in JavaScript.

export const delay = async (millis: number) =>
  new Promise((resolve) => setTimeout(resolve, millis));

Example Usage

// Wait for 2 seconds
await delay(2000);

Bonus: Node.js

If in node, you could also just do

import { setTimeout as delay } from 'node:timers/promises';

// Wait for 2 seconds
await delay(2000);