最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why use `eventEmitter` instead of promise - Stack Overflow

programmeradmin5浏览0评论

I've found the following example in a book I'm reading:

function User() {
    EventEmitter.call(this);
    this.addUser = function (username, password) {
        // add the user
        // then emit an event
        this.emit("userAdded", username, password);
    };
}

var user = new User();
var username = "colin";
var password = "password";

user.on("userAdded", function(username, password) {
    console.log("Added user " + username);
});

user.addUser(username, password);

It seems to me that using EventEmitter is pletely redundant here. Promises would do a much better job:

function User() {
    this.addUser = function (username, password) {
        return new Promise(function (resolve) {
            // add the user
            // and resolve
            resolve();
        });
    };
}

and the usage:

user.addUser(username, password).then(function(username, password) {
    console.log("Added user " + username);
});

Does using EventEmitter have any advantages over using Promises or it's simply the code from the time when Promises where not available? Or is this style is not weled in node.js?

I've found the following example in a book I'm reading:

function User() {
    EventEmitter.call(this);
    this.addUser = function (username, password) {
        // add the user
        // then emit an event
        this.emit("userAdded", username, password);
    };
}

var user = new User();
var username = "colin";
var password = "password";

user.on("userAdded", function(username, password) {
    console.log("Added user " + username);
});

user.addUser(username, password);

It seems to me that using EventEmitter is pletely redundant here. Promises would do a much better job:

function User() {
    this.addUser = function (username, password) {
        return new Promise(function (resolve) {
            // add the user
            // and resolve
            resolve();
        });
    };
}

and the usage:

user.addUser(username, password).then(function(username, password) {
    console.log("Added user " + username);
});

Does using EventEmitter have any advantages over using Promises or it's simply the code from the time when Promises where not available? Or is this style is not weled in node.js?

Share Improve this question asked Dec 14, 2016 at 10:52 Max KoretskyiMax Koretskyi 106k68 gold badges353 silver badges516 bronze badges 2
  • 1 What if you want to be notified when multiple users are added with the same hanfler? Promise's wont work there. A promise can only be resolved with a value once. – ste2425 Commented Dec 14, 2016 at 11:02
  • 1 As for me, main differences between EventEmitter and Promise in point, that Promise can be fulfilled only once whereas events can be triggered any number of times. – Denis Lisitskiy Commented Dec 14, 2016 at 11:03
Add a ment  | 

2 Answers 2

Reset to default 10

Main differences between EventEmitter and Promise in point, that Promise can be fulfilled only once whereas events can be triggered any number of times

I do not understand in which scenario it bees reality what Denis wrote. Each time addUser is called, a new promise is returned. In event driven logic a new event is triggered. Main difference I see is that a promise, whilst more performant (in my experience), is sort of "local". You have singular linear path from where method was called. Meanwhile when using events, you can have multiple other ponents listen to the event. So each time a user gets added, all of them get notified and run their logic. Another thing is that with events you can define what happens once and then no matter where the method which triggers the event is called, the event listener callback fires.

发布评论

评论列表(0)

  1. 暂无评论