1

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);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

1

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
Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90