My class has a method that does something like this:
async foo(..., callback){
do{
for(...){
await bar1();
await bar2();
}
}
while(...);
callback();
}
I want to be able to interrupt the loop on command, so this was my approach at it:
async foo(..., callback){
this.stop = false; // Reset the property
do{
for(...){
await bar1();
await bar2();
}
}
while(!this.stop && ...);
callback();
}
stopTheLoop(){
this.stop = true;
}
And it looked fine to me. So I test it like:
myObject.foo(..., () => {
console.log("Finished!");
});
console.log("Async test");
setTimeout(() => {
myObject.stopTheLoop();
}, 1000);
The behaviour is that i see printed "Async test", which means that the foo() call is correcrly not blocking code execution on the main thread, so i think it's safe to say that the setTimeout function gets called. But the do-while loop never ends, and i never see my "Finished!" callback.
I might not have fully understood async/await in ES6, what am I missing?