milliseconds to wait
value to resolve with
TimeoutPromise that resolves after the delay
await wait(1000); // Wait 1 second
console.log('One second has passed');
const timeout = wait(1000);
timeout.clear(); // Clears the timeout
const someVal = await wait(100, 'some value');
console.log(someVal); // 'some value'
// Add delay between operations
for (const item of items) {
await processItem(item);
await wait(100); // Throttle processing
}
// Retry with backoff
async function retryOperation(fn: () => Promise<any>, attempts = 3) {
for (let i = 0; i < attempts; i++) {
try {
return await fn();
} catch (error) {
if (i === attempts - 1) throw error;
await wait(1000 * Math.pow(2, i)); // Exponential backoff
}
}
}
Waits for the specified number of milliseconds before resolving with the optional value.
Can be cleared using the
clearmethod.