Alireza Davoodi
Alireza Davoodi

Reputation: 769

Using promises with event emitters

I want promisify event emitter in an node.js XMPP client, this is my code:

this.watiForEvent = function (eventEmiter, type) {
    return new Promise(function (resolve) {
        eventEmiter.on(type, resolve);
    })
};

this._response = function () {
    return this.watiForEvent(this.client, 'stanza');
};

in above code I promisify the XMPP stanza event emitter and use it something like this

CcsClient.prototype.responseHandler = function () {
    var _this = this;

    return _this._response().then(function (stanza) {

        _this.responseLimit--;

        if (stanza.is('message') && stanza.attrs.type !== 'error') {
            var messageData = JSON.parse(stanza.getChildText("gcm"));

            switch (messageData.message_type) {
                case 'control':
                    if (messageData.control_type === 'CONNECTION_DRAINING') {
                        _this.draining = true;
                    }

                    return;

                case 'ack':
                    return {
                        messageId: messageData.message_id,
                        from: messageData.from
                    };


                    gcm.responseHandler().then(function (options) {
                        console.log(options);
                    }).
                    catch (function (error) {
                        console.log(error);
                    });

My problem is stanza event just called one time. Maybe promise is not good?

Upvotes: 0

Views: 2745

Answers (1)

amin
amin

Reputation: 3852

Es6-Promises have 3 states, pending, resolved and rejected.
In the code you provided eventEmitter is resolving the promise multiple times.

eventEmiter.on(type, resolve);

Thats is not going to work, because once a promise is rejected or resolved its state can not be changed, you can not resolve a promise multiple times.

I suggest using the observable pattern:

function observable(){
    var cbs = { }; // callbacks;
    return {
        on: function(type,cb) {
            cbs[type] = cbs[type] || [];
            cbs[type].push(cb);
        },
        fire: function(type){
            var arr = cbs[type] || [];
            var args = [].slice.call(arguments,1);
            arr.forEach(function(cb){
               cb.apply(this,args);
            });
        }
    }
}

Upvotes: 3

Related Questions