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

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations