Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In general terms: what is the difference between a handler and a function?

From this answer (What is a handler) I understand that a handler is:

A handler is a routine/function/method which is specialized in a certain type of data or focused on certain special tasks.

But then I'm asking myself: why not replace the word handler with the word function? What's the difference? The only difference I can tell is that functions do not need to have arguments, which means they do not need to be focused on a certain type of data. And some functions -- without arguments -- are not focused on specialized tasks.

But other than that, what is the difference between a handler and a function?

Some more background:

I find the concept of handler difficult to understand still (it could be because I'm Dutch and there is not a word for it in Dutch according to Google Translate). Checking out the definition helps a bit but not fully. enter image description here

like image 635
Melvin Roest Avatar asked Dec 08 '22 18:12

Melvin Roest


2 Answers

A handler is simply a more specific term. What's the difference between a fruit and an apple?

All handlers in JS are functions, but not all functions are handlers. It's a way of being more precise in your terms. If I say "a function", I could be referring to any function. If I say "a handler", then I'm referring specifically to a function which is intended to respond to some event that is occurring, usually asynchronously like a mouse click or in Express a request being processed.

like image 94
Paul Avatar answered Dec 11 '22 10:12

Paul


Handlers are indeed just functions. Handlers are functions that have the intended behaviour of being called right after an event is fired, for example when an img is clicked or when you scroll past a certain element on the page.

like image 45
glhrmv Avatar answered Dec 11 '22 11:12

glhrmv