Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
await-timeout
Advanced tools
Readme
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 'Error'
await Timeout.set(1000, 'Error');
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)
.then(() => Promise.reject('Timeout'))
]);
} finally {
timer.clear();
}
Constructs new timeout instance. It does not start timer but creates variable for timer manipulation.
const timer = new Timeout();
Note: having separate variable is useful for clearing timeout in
finally
block
Promise
Starts new timer like setTimeout()
and returns promise. The promise will be resolved after ms
milliseconds:
const timer = new Timeout();
timer.set(1000)
.then(() => console.log('1000 ms passed.'));
If you need to reject after timeout:
timer.set(1000)
.then(() => {throw new Error('Timeout')});
Or reject with custom error:
timer.set(1000)
.then(() => {throw new MyTimeoutError()});
The second parameter message
is just convenient way to reject with new Error(message)
:
timer.set(1000, 'Timeout');
// is equivalent to
timer.set(1000).then(() => {throw new Error('Timeout')});
If you need to just wait some time - use static version of .set()
:
await Timeout.set(1000);
Promise
Wraps existing promise with timeout:
const promise = fetch('https://example.com');
const timeoutedPromise = Timeout.wrap(promise, 1000, 'Timeout');
Actually it is a shortcut for:
const promise = fetch('https://example.com');
const timer = new Timeout();
try {
const timeoutedPromise = 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();
}
}
Before making this library I've researched many 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
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.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).