0

I have a code which makes an ajax call, which has a callback. Is there a way i can replace the callback with an event emitter.

Below is my code.

 var _getPoll = function(params){
    var url = "http://localhost/poll"

    console.log(url);

    request({
        headers: {
            accept: 'application/json'
        },
        uri: url,
        method: 'GET'
    }, function(err, response, body){
        body = JSON.parse(body);
        console.log(body);
    })
}

Is it possible to replace the callback with an EventEmitter like below.

function(err, response, body){
    body = JSON.parse(body);
    console.log(body);
}

Replace

this.emit('jsonResponse', err, response, body);
UnderTaker
  • 853
  • 3
  • 13
  • 22
  • I'm curious. What would be the benefit of not using a callback? – KJ Price Nov 13 '14 at 18:11
  • @KJPrice: Next callbacks are always pain... that's the reason... – UnderTaker Nov 13 '14 at 18:14
  • I don't understand the question. The EventEmitter is a system that lets you assign a callback to fire when an event happens. –  Nov 13 '14 at 18:25
  • Related: [Callback hell in nodejs?](http://stackoverflow.com/questions/18095107/callback-hell-in-nodejs) – Jonathan Lonowski Nov 13 '14 at 18:28
  • ...Given your example code at the bottom, you're still using a callback function. So why not give it a name, and use the named function? So you'd have `function foobar(err, response, body){`, and then just put `foobar` where the current anonymous function is. Or you can have `_getPoll` receive it as an argument or as one of the `params`. –  Nov 13 '14 at 18:33

1 Answers1

1

Is there a way i can replace the callback with an event emitter.

Yes. In fact, request already does return you an event emitter. You'd listen to it instead of passing your callback directly into the request() call. It will emit a complete event with the body as soon as the response stream has ended.

 function _getPoll(params) {
    var url = "http://localhost/poll";

    console.log(url);

    request({
        headers: {
            accept: 'application/json'
        },
        uri: url,
        method: 'GET'
    }).on("complete", function(response, body) {
        console.log(JSON.parse(body));
    });
}

Is it possible to replace the callback with this.emit('jsonResponse', err, response, body)

I'm not sure whether you mean the right thing here. No, you cannot replace the callback with anything but a different callback. You can not pass an even emitter object, and you can not pass the result of calling this.emit('jsonResponse', err, response, body) either. You might do something like

var e = new EventEmitter();
request({…}, function(err, response, body){
    if (err)
        e.emit("error", err)
    else
        e.emit('jsonResponse', response, JSON.parse(body));
})

Next callbacks are always pain... that's the reason...

I think what you are actually looking for are promises. Of course, they're still callbacks (just as event emitters are), but they are much more composable.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375