I am struggling to get the complete hold of the code below. I want to understand the working of emit
.
Here is my understanding of the instances of all emits mentioned in the code below.
profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
It executes the error function.( But I am not sure where error function is defined in the code.
response.on('data', function (chunk) { body += chunk; profileEmitter.emit("data", chunk); });
This emits a data event function as defined above.All fine ! But what is second parameter. As per the docs this argument should be a listener , but all it is a parameter - of "anonymous function" defined ahead of data.
try { //Parse the data var profile = JSON.parse(body); profileEmitter.emit("end", profile); } catch (error) { profileEmitter.emit("error", error); }
The first emit in the try block has a profile
variable this time. The second emit in the catch block has a error
as second arg. Well ! All confused.
var EventEmitter = require("events").EventEmitter;
var http = require("http");
var util = require("util");
function Profile(username) {
EventEmitter.call(this);
profileEmitter = this;
//Connect to the API URL (http://teamtreehouse.com/username.json)
var request = http.get("http://example.com/" + username + ".json", function(response) {
var body = "";
if (response.statusCode !== 200) {
request.abort();
//Status Code Error
profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
}
//Read the data
response.on('data', function (chunk) {
body += chunk;
profileEmitter.emit("data", chunk);
});
response.on('end', function () {
if(response.statusCode === 200) {
try {
//Parse the data
var profile = JSON.parse(body);
profileEmitter.emit("end", profile);
} catch (error) {
profileEmitter.emit("error", error);
}
}
}).on("error", function(error){
profileEmitter.emit("error", error);
});
});
}
util.inherits( Profile, EventEmitter );
module.exports = Profile;
The EventEmitter.emit()
function says just what it says: it sends an event to the listeners that are registered for said event.
The arguments after the first one (the event type) are just arguments of the event, they're not listeners.
So your first call just send an error
event, with an attached Error
argument that describes the error.
The second call sends a data
event, along with the chunk of data that was just received.
The third call sends an end
event, along with the decoded profile.
The last call sends an error
event, along with the error received from catch
.
the all concept of EventEmitter is same as Observer pattern
in java (if you comes from java world), you observe the event you interested in your example data
event, and everytime you emit a data
event by emit('data', {data})
, it will be handled by the event handler which subscribe the event by on('data', handlerfunction)
.
error
is a special event in nodejs, if you don't handle it, it will just blow up your process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.