Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFaisal Julaidan
13,944 PointsCallbacks vs Event handlers
I really get confused and frustrated when developers use these two terms. some of them use callback frequently and some use event handlers, but i don't see any different between them. it all handles something after something is happened like mouse clicking or after HTTP request it is been done.
could you please what is the main different between callback and event handlers and is event handlers same as the terms events without the word "handler"
1 Answer
Kyle Kirst
9,884 PointsVery simply -> An event handler is a type of callback.
An event handler is a callback to an event the occurred like a mouse click. More specifically, an event handler is the callback method that is passed into a event listener. When the listener is triggered, it fires its callback, or in this case, the event handler.
Callbacks methods do not need to be associated with events. In fact, generally speaking, when someone uses the term callback, they are referring to a function that gets passed as a parameter into a method which is fired when the method completes.
I hope this clarifies the difference. Good luck!
EDIT:
Types of callbacks
I use callback when using the async library a lot. It's really popular, and very powerful when it comes to asynchronous work. I use it a lot when doing Node.js work. You can find it here: https://github.com/caolan/async
In the example below, you are calling the "callback" method when you have completed the try statement, or calling it with the error when it has failed. In this example, it helps signify when processing, if you will, has been completed on this particular item in the object or array.
You are also passing in a method 'crazyMonkey' at the very end that is a callback function and will get fired when async.forEachOf is complete.
Here is an example:
var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
var configs = {};
async.forEachOf(obj, function (value, key, callback) {
fs.readFile(__dirname + value, "utf8", function (err, data) {
if (err) return callback(err);
try {
configs[key] = JSON.parse(data);
} catch (e) {
return callback(e);
}
callback();
})
}, crazyMonkey)
function crazyMonkey(err) {
if (err) console.error(err.message);
// configs is now a map of JSON data
doSomethingWith(configs);
}
Faisal Julaidan
13,944 Pointsthanks for your answer it is obvious now. One more thing what are other types of callback, if you could just mention one or two I would really appreciate that.
Kyle Kirst
9,884 PointsKyle Kirst
9,884 PointsVery simply -> An event handler is a type of callback.
An event handler is a callback to an event the occurred like a mouse click. More specifically, an event handler is the callback method that is passed into a event listener. When the listener is triggered, it fires its callback, or in this case, the event handler.
Callbacks methods do not need to be associated with events. In fact, generally speaking, when someone uses the term callback, they are referring to a function that gets passed as a parameter into a method which is fired when the method completes.
I hope this clarifies the difference. Good luck!