AbortError if the signal is already aborted or becomes aborted.
Signature
Parameters
The duration to wait in milliseconds.
Delay options.
Returns
A promise that resolves after the specified duration.
Throws
AbortErrorif the signal is aborted
Learn more about Mintlify
Enter your email to receive updates about new features and product releases.
Resolves after a specified duration in milliseconds
AbortError if the signal is already aborted or becomes aborted.
function delay(ms: number, options?: StandardOptions): Promise<void>
Show properties
AbortError if the signal is abortedimport { delay } from '@temelj/async';
console.log('Starting...');
await delay(1000);
console.log('1 second later');
import { delay } from '@temelj/async';
for (let i = 1; i <= 5; i++) {
console.log(`Step ${i}`);
await delay(500);
}
import { delay } from '@temelj/async';
async function processWithRateLimit(items: string[]) {
for (const item of items) {
await processItem(item);
// Wait 100ms between items
await delay(100);
}
}
import { delay } from '@temelj/async';
const controller = new AbortController();
const delayTask = delay(5000, { signal: controller.signal });
// Cancel after 1 second
setTimeout(() => controller.abort(), 1000);
try {
await delayTask;
console.log('Delay completed');
} catch (error) {
if (error instanceof AbortError) {
console.log('Delay cancelled');
}
}
import { delay } from '@temelj/async';
const controller = new AbortController();
const timeoutPromise = delay(10000, { signal: controller.signal });
const workPromise = doWork();
try {
await Promise.race([
workPromise,
timeoutPromise.then(() => {
throw new Error('Timeout exceeded');
}),
]);
} finally {
// Cancel the timeout if work completes first
controller.abort();
}
import { delay } from '@temelj/async';
let abortController: AbortController | null = null;
async function handleInput(value: string) {
// Cancel previous delay
abortController?.abort();
// Create new delay
abortController = new AbortController();
try {
await delay(300, { signal: abortController.signal });
// Process input after user stops typing
await processInput(value);
} catch (error) {
if (error instanceof AbortError) {
// Input changed, ignore
return;
}
throw error;
}
}
import { delay } from '@temelj/async';
async function retryWithBackoff<T>(
fn: () => Promise<T>,
maxAttempts: number = 3
): Promise<T> {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
try {
return await fn();
} catch (error) {
if (attempt === maxAttempts - 1) {
throw error;
}
// Exponential backoff: 1s, 2s, 4s
const backoff = Math.pow(2, attempt) * 1000;
await delay(backoff);
}
}
throw new Error('Should not reach here');
}
import { delay } from '@temelj/async';
async function animateProgress(duration: number) {
const steps = 100;
const stepDuration = duration / steps;
for (let i = 0; i <= steps; i++) {
updateProgressBar(i);
await delay(stepDuration);
}
}
await animateProgress(2000); // 2 second animation