2021-01-31

How to stop a an asynchronous function immediately at a certain time?

I want to run a script like this:

while(Date.now() < dateToStop){
    doSomething()
    await new Promise(r => setTimeout(r, 2000));
    doSomething()
    await new Promise(r => setTimeout(r, 2000));
}

The only thing is, I want to exit the loop the "instant" Date.now() < dateToStop is false, whereas this function could exit 4 seconds after due to its asynchronous nature nature.

How do I solve this?



from Recent Questions - Stack Overflow https://ift.tt/3cs20ho
https://ift.tt/eA8V8J

No comments:

Post a Comment