A TypeScript library for implementing customizable polling mechanisms with adjustable timeouts and intervals.
npm install ts-wait``Shell`
npm install ts-wait
`Typescript
import { Wait } from 'ts-wait';
// Basic usage
await new Wait
.pollingEvery(1000)
.withTimeout(20000)
.until(document => document.status === 'COMPLETED')
// With maximum attempts limit
await new Wait
.pollingEvery(1000)
.withMaxAttempts(10) // Stop after 10 polling attempts
.until(document => document.status === 'COMPLETED')
// With exponential backoff
await new Wait
.pollingEvery(1000) // Initial interval
.withExponentialBackoff(2, 10000) // Multiply by 2 each time, max 10 seconds
.withTimeout(60000)
.until(document => document.status === 'COMPLETED')
// Combining features
await new Wait
.pollingEvery(500)
.withMaxAttempts(15)
.withExponentialBackoff(1.5) // Gradually increase with 1.5x multiplier
.withTimeout(30000)
.until(document => document.status === 'COMPLETED')
`
A class that represents the Wait library.Constructor
new Wait(call: () => Promise: Creates a new Wait instance with the provided asynchronous function.##### Methods:
withTimeout(timeout: number): Wait Sets the timeout for waiting in milliseconds (default: 30000)pollingEvery(interval: number): Wait Sets the polling interval in milliseconds (default: 1000).withMessage(message: string): Wait Sets a custom error message.withMaxAttempts(maxAttempts: number): Wait Sets the maximum number of polling attempts. The polling will stop when either the timeout is reached or the maximum number of attempts is exceeded, whichever comes first.withExponentialBackoff(multiplier: number = 2, maxInterval?: number): Wait Enables exponential backoff for the polling interval. The interval will be multiplied by the specified multiplier after each attempt. Optionally, you can set a maximum interval to cap the growth.
- multiplier: The factor by which to multiply the interval (default: 2)
- maxInterval: Optional maximum interval in milliseconds to prevent unbounded growthasync until(condition: (it: T) => boolean): Promise