Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling timer.set() several times #2

Closed
aleksandr-oleynikov opened this issue Sep 14, 2018 · 2 comments
Closed

Calling timer.set() several times #2

aleksandr-oleynikov opened this issue Sep 14, 2018 · 2 comments
Labels

Comments

@aleksandr-oleynikov
Copy link

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?

@vitalets vitalets changed the title Some points to discuss Calling timer.set() several times Sep 17, 2018
@vitalets
Copy link
Owner

Good catch!
I think the simplest solution is to automatically clear existing timer if .set is called again. So in both your examples nothing will be printed.
If you need to re-set timer on new timeout, you should provide new .then:

timer.set(5000).then(() => console.log('TIMER 5'));
setTimeout(() => {
    timer.set(13000).then(() => console.log('TIMER 13'));
}, 2000);

This will log "TIMER 13".
Is it suitable for your case?

@aleksandr-oleynikov
Copy link
Author

I think that's just what I need because in my case I need to postpone timer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants