Skip to content

Instantly share code, notes, and snippets.

@exvion
Last active December 21, 2023 09:11
Show Gist options
  • Save exvion/76aa8d462cb79176980df306969f2140 to your computer and use it in GitHub Desktop.
Save exvion/76aa8d462cb79176980df306969f2140 to your computer and use it in GitHub Desktop.
events and interval
import { EventEmitter } from 'events';
class Client extends EventEmitter {
interval
constructor() {
super();
this.interval = setInterval(() => { console.log("emit 1"); this.emit("onOrder", "1") }, 3000);
}
send() {
return new Promise((resolve, reject) => {
const check_order = (order) => {
if (order === "1") {
this.removeListener('onOrder', check_order);
clearInterval(this.inverval);
resolve(order);
}
}
this.on("onOrder", check_order)
})
}
}
const client = new Client();
client.send().then((x) => console.log("result " + x))
// Output:
// emit 1
// result 1
// emit 1
// emit 1
// emit 1
// emit 1
// emit 1
// emit 1
// emit 1
// emit 1
// emit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment