To prevent Node.js from exiting while waiting for a callback, we use the EventEmitter
to wait for the event.
For instance, we write
const eventEmitter = new process.EventEmitter();
client.connect((err) => {
eventEmitter.emit("myEvent", { something: "Bla" });
});
eventEmitter.on("myEvent", (myResult) => {
process.stdout.write(JSON.stringify(myResult));
});
to create an EventEmitter
objbect.
We emit the myEvent
event with emit
with { something: "Bla" }
as the payload.
Then we listen for the myEvent
event with on
and get the payload from myResult
.