await-timeout, currently at version 1.1.1, offers a Promise-based API for handling `setTimeout` and `clearTimeout` in JavaScript, making it particularly useful within `async/await` constructs. It simplifies common asynchronous patterns such as adding timeouts to network requests or other long-running operations. The library provides both static methods like `Timeout.set(ms)` for simple delays and instance methods (`new Timeout().set(ms)`) for more controlled scenarios, including wrapping existing promises with timeouts using `Timeout.wrap(promise, ms, rejectReason)`. A key differentiator is its emphasis on proper resource management, advising the use of `.clear()` within `finally` blocks to prevent unhandled promise rejections or 'unexpected effects' when dealing with `Promise.race`. While a specific release cadence isn't detailed, its stable 1.x version implies a mature and focused utility for precise timeout management in asynchronous codebases.
npm install await-timeoutVerified import paths — ran on the pinned version, not inferred.
Demonstrates using `await-timeout` to add a timeout to a fetch request with `Promise.race` and proper cleanup, alongside a simple static delay.
Ensure `timer.clear()` is called within a `try...finally` block after `Promise.race` or any operation involving an instance-based timer.
Always chain a `.catch()` to the promise returned by `timer.set(delay, rejectReason)` or wrap the `await` call in a `try...catch` block if the promise is configured to reject.
Familiarize yourself with the internal implementation of `wrap` (as shown in the README) to understand how it uses `Promise.race` and `timer.clear()` to ensure proper behavior.
Add a `.catch()` block to the promise returned by `Timeout.set()` or wrap the `await Timeout.set()` call in a `try...catch` statement to handle the rejection.
Use `const timer = new Timeout();` to create an instance, ensuring `Timeout` is correctly imported as a class: `import Timeout from 'await-timeout';`.
No dependency data recorded yet.