Sleep in Javascript

Recently, I was writing a script in TypeScript where I was calling an API a few hundreds of times. I wanted a brief delay between the API calls, when I found out that there is no built-in sleep in Javascript.

Fortunately, it was easy enough to implement:

export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

Sample usage:

for (const item of items) {
    // TODO: do some work

    // wait for 100ms
    await sleep(100);
}

source