Wraps an asynchronous function with a timeout.
If the wrapped function does not resolve or reject within the specified time, the returned promise rejects with a "Timeout" error.
The type of the async function.
The asynchronous function to wrap.
Timeout duration in milliseconds.
A new function that returns a promise which rejects if the timeout is exceeded.
const fetchData = async () => { await new Promise(r => setTimeout(r, 2000)); return "Done";};const result = withTimeout(fetchData, 1000)();result.catch(err => console.log(err.message)); // "Timeout" Copy
const fetchData = async () => { await new Promise(r => setTimeout(r, 2000)); return "Done";};const result = withTimeout(fetchData, 1000)();result.catch(err => console.log(err.message)); // "Timeout"
1.1.0
Wraps an asynchronous function with a timeout.
If the wrapped function does not resolve or reject within the specified time, the returned promise rejects with a "Timeout" error.