in my real scenario I need to stop a process that is running in a loop when a stop signal comes from a websocket.
with a design I use now I do something like this:
async function process() {
Promise.all([
detonator(),
engine(),
]).catch(e => {
if (e.message === 'detonated')
console.log('it works');
// but it's not caught at all
})
}
async function detonator() {
setTimeout(() => {
console.log('throwing e');
throw new Error('detonated');
}, 1500);
}
async function engine() {
while (true) {
await wait(500);
}
}
async function wait(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
process();
so as you can see I want to halt the engine
method in the process
from a remote when I throw an exception in that remote -> that would be mine message received from ws.
however I need to catch that exception in that catch block in process
method..which is not happening.
I assume I just need to .bind()
contexts properly, but idk, I have forgot some js magic.
Please avoid commenting when you don't understand me.
appreciated