Back to Code Bytes
1 min read
sleep()

Description

This sleep function creates a promise that resolves after a specified number of milliseconds, simulating a delay. It’s particularly useful in unit testing to mimic asynchronous operations without making actual API calls. By using await sleep(2000), you can pause execution for 2 seconds, allowing you to test timing-dependent behavior or simulate network latency in a controlled manner.

Code Byte

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(2000);