Message from JavaScript discussions

January 2019

— You wait for event after you set it, and then it goes on

— 

class Halter {
constructor() {
this.waiting = [];
}
hlt() {
new Promise(resolve =>
this.waiting.push(resolve));
}
continue(...a) {
return Promise.all(this.waiting.map(x =>
x(...a)));
}
}

— It works exactly like your example

— No, foo is called before

— You can call it after too

— This order is not natural/intuitive

— It's the same as your example

— Wait, missing a thing

— setTimeout(() => h.continue(), 1000);
h.hlt();

console.log('stuff')
; //after 1 sec

— You would need to await

— But then it would work

— setTimeout(() => h.continue(), 1000);
await h.hlt();

console.log('stuff'); //after 1 sec