I am reading this post: write async function with EventEmitter
Is the following piece of code async?
var event = new events.EventEmitter();
event.on("done", cb);
I am reading this post: write async function with EventEmitter
Is the following piece of code async?
var event = new events.EventEmitter();
event.on("done", cb);
In the given code in question, you are subscribing for an event. When you call event.emit("done")
, node execute the given callback in the same order they are subscribed.
Example
var event = new events.EventEmitter();
event.on("done",() => {
console.log(("notified 1"))
});
event.on("done",() => {
setImmediate(() => {
console.log("async")
});
console.log(("notified 2"))
});
event.on("done",async () => {
console.log(("notified 3"))
});
console.log("before firing an event");
event.emit("done");
console.log("after firing an event");
Output
before firing an event
notified 1
notified 2
notified 3
after firing an event
async