
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
await-timeout
Advanced tools
npm install await-timeout --save
Just wait some time:
import Timeout from 'await-timeout';
// wait 1000 ms and resolve
await Timeout.set(1000);
// wait 1000 ms and reject with 'Timeout!'
await Timeout.set(1000, 'Timeout!');
Use Timeout instance inside try...finally block to make proper cleanup:
import Timeout from 'await-timeout';
const timer = new Timeout();
try {
await Promise.race([
fetch('https://example.com'),
timer.set(1000, 'Timeout!')
]);
} finally {
timer.clear();
}
Without a timer cleanup you may get unexpected effects in you code - as all promises in
Promise.raceare get fulfilled.
Constructs new timeout instance. It does not start timer but creates variable for timer manipulation.
const timer = new Timeout();
Note: having separate
timervariable is useful for clearing timeout infinallyblock
PromiseStarts new timer like setTimeout() and returns promise. The promise will be resolved after delay milliseconds:
const timer = new Timeout();
timer.set(1000)
.then(() => console.log('1000 ms passed.'));
If you provide rejectReason - a timer promise will be rejected with specified reason:
// rejects with Error: Timeout after 1000 ms:
timer.set(1000, 'Timeout after 1000 ms');
// above is actually shortcut for:
timer.set(1000).then(() => Promise.reject(new Error('Timeout after 1000 ms')));
If you need to just wait some time - use static version of .set():
await Timeout.set(1000);
PromiseWraps existing promise with timeout:
async function fetchWithTimeout() {
const promise = fetch('https://example.com');
return Timeout.wrap(promise, 1000, 'Timeout');
}
Actually it is a shortcut for:
async function fetchWithTimeout() {
const timer = new Timeout();
try {
const promise = fetch('https://example.com');
return await Promise.race([
promise,
timer.set(1000, 'Timeout')
]);
} finally {
timer.clear();
}
}
Clears existing timeout like clearTimeout().
const timer = new Timeout();
timer.set(1000)
.then(() => console.log('This will never be called, because timeout is cleared on the next line'));
timer.clear();
With ES7 async / await .clear() can be used in finally block:
async function foo() {
const timer = new Timeout();
try {
// some async stuff
} finally {
timer.clear();
}
}
?Number|?TimeoutReturns result of setTimeout call. That is Number timeout id in browser
and Timeout instance in Node.js.
?NumberReturns last delay value used. Delay is useful for generating reject reason:
const timer = new Timeout();
timer.set(1000, () => new Error(`Timeout: ${timer.delay}`));
Before making this library I've researched several similar packages on Npm. But no one satisfied all my needs together:
setTimeout / clearTimeout. I get used to these functions and would like to have mirror syntax.MIT @ Vitaliy Potapov
FAQs
A Promise-based API for setTimeout / clearTimeout
The npm package await-timeout receives a total of 170,606 weekly downloads. As such, await-timeout popularity was classified as popular.
We found that await-timeout demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.