vitalets / await-timeout Goto Github PK
View Code? Open in Web Editor NEWA Promise-based API for setTimeout / clearTimeout
A Promise-based API for setTimeout / clearTimeout
It would also be nice to consider supporting AbortController which would allow the API to be web compatible (cancelling a timer and a fetch at the same time for instance):
const controller = new AbortController()
const timer = Timeout.set(1000, { signal: controller.signal });
signal.abort();
From #3 (comment)
Hello, I have a small quesiton about this code:
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.race
are get fulfille
Why the timer need to be cleanup? race()
only accept the first settled promise.
Hello!
I've found some unobvious behavior of await-timeout
during its inclusion in our project. And want to discuss them with you and community.
So the first my example is:
var awaitTimeout = require("await-timeout");
var timer = new awaitTimeout();
timer.set(5000).then(() => console.log('TIMER'));
setTimeout(() => {
timer.set(13000);
}, 2000);
This block of code will log TIMER
after 5 sec. But at first I thought that it logs TIMER
after 15 (2 + 13) sec. because second timer.set(13000)
call postpones timer. But in result this second call doesn't influence.
Next example:
var awaitTimeout = require("await-timeout");
var timer = new awaitTimeout();
timer.set(5000).then(() => console.log('TIMER1'));
setTimeout(() => {
timer.set(5000).then(() => console.log('TIMER2'));
timer.clear();
}, 2000);
It will log TIMER1
, but TIMER2
won't never be logged.
If I understood it correct setting timer is available only once until timer clearing.
Maybe timer has to throw an error in that case then.
And I think it would be great to clarify this cases in Readme.
What do you think?
Wouldn't it be better to have:
const timer = Timeout.set(1000); // set the timer
Timeout.clear(timer); // clear the timer
?
report by UglifyJs
It runs the test() function start to end, even though it times out. How do I make it abort the function that timed out? Similar to what Bluebird does with the {cancellation: true}
feature, but using async/await.
// const Promise = require('bluebird');
// Promise.config({cancellation: true});
const Timeout = require('await-timeout');
const delay = ms => new Promise(_ => setTimeout(_, ms))
async function test() {
console.log('start')
await delay(2000)
console.log('end')
}
async function run() {
const timer = new Timeout();
try {
await Promise.race([
test(),
timer.set(1200, 'Timeout!')
]);
} catch (e) {
console.log(e);
} finally {
timer.clear();
}
}
run();
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.