Skip to main content
Source: timeout.ts • Tests: timeout.test.ts
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.

Timeout

Add timeouts to promises and async operations. Essential for preventing hung requests, managing long-running operations, and implementing cancellation in Ethereum applications.

Overview

Timeout utilities provide:
  • Promise timeouts: Race promises against time limit
  • Cancellation: AbortSignal support
  • Function wrapping: Create timeout-wrapped versions
  • Deferred promises: Manual promise control
  • Retry integration: Timeout + retry patterns

Basic Usage

Simple Timeout

Wrap a promise with timeout:

Custom Timeout Message

Timeout Options

TimeoutOptions

Timeout Functions

withTimeout

Add timeout to any promise:

wrapWithTimeout

Create timeout-wrapped functions:

sleep

Async delay with optional cancellation:

createDeferred

Manual promise control:

executeWithTimeout

Timeout with built-in retry:

Cancellation with AbortSignal

Basic Cancellation

Multiple Operations

Share abort controller across operations:

Real-World Examples

RPC Call with Timeout

Prevent hung RPC calls:

Transaction Submission

Timeout transaction submission:

Parallel Operations with Global Timeout

Timeout entire batch:

Race Against Multiple Providers

Use fastest provider:

User-Initiated Cancellation

Cancel long-running operation:

Combining with Other Utils

Timeout + Retry

Retry with timeout per attempt:

Timeout + Polling

Add timeout to polling:

Timeout + Rate Limiting

Timeout rate-limited operations:

Error Handling

TimeoutError

Catch timeout-specific errors:

Cleanup on Timeout

Perform cleanup when timeout occurs:

Best Practices

Choose Appropriate Timeouts

  • Fast operations (balance, blockNumber): 5000-10000ms
  • Medium operations (calls, receipts): 10000-30000ms
  • Slow operations (logs, traces): 30000-60000ms

Always Handle TimeoutError

Use AbortSignal for Cleanup

When operations support AbortSignal, use it for proper cleanup:

Per-Operation vs Global Timeout

  • Per-operation: Each request has own timeout
  • Global: Entire batch has single timeout

API Reference

withTimeout

wrapWithTimeout

sleep

createDeferred

executeWithTimeout

TimeoutError

See Also