Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between hook and callback?

Tags:

callback

hook

By reading some text, especially the iOS document about delegate, all the protocol method are called hook that the custom delegate object need to implement. But some other books, name these hook as callback, what is the difference between them? Are they just different name but the same mechanism? In addition to Obj-C, some other programming languages, such as C, also got the hook, same situation with Obj-C?

like image 267
coanor Avatar asked Jun 18 '12 16:06

coanor


People also ask

Are react hooks callbacks?

The React useCallback Hook returns a memoized callback function. Think of memoization as caching a value so that it does not need to be recalculated. This allows us to isolate resource intensive functions so that they will not automatically run on every render.

What is a webhook callback?

Webhooks are user-defined HTTP callbacks. They are triggered by some event in a web application and can facilitate integrating different applications or third-party APIs, like Twilio.

What is a callback?

A callback is a function passed into another function as an argument to be executed later. A high-order function is a function that accepts another function as an argument. Callback functions can be synchronous or asynchronous.

What does hook mean in coding?

In programming, a hook is a place and usually an interface provided in packaged code that allows a programmer to insert customized programming. For example, a programmer might want to provide code that analyzed how often a particular logic path was taken within a program.


2 Answers

The terminology here is a bit fuzzy. In general the two attempt to achieve similar results.

In general, a callback is a function (or delegate) that you register with the API to be called at the appropriate time in the flow of processing (e.g to notify you that the processing is at a certain stage)

A hook traditionally means something a bit more general that serves the purpose of modifying calls to the API (e.g. modify the passed parameters, monitor the called functions). In this meaning it is usually much lower level than what can be achieved by higher-level languages like Java.

In the context of iOS, the word hook means the exact same thing as callback above

like image 131
Attila Avatar answered Oct 12 '22 14:10

Attila


Let me chime in with a Javascript answer. In Javascript, callbacks, hooks and events are all used. In this order, they are each higher level concepts than the other.

Unfortunately, they are often used improperly which leads to confusion.

Callbacks

From a control flow perspective, a callback is a function, usually given as an argument, that you execute before returning from your function.

This is usually used in asynchoronous situations when you need to wait for I/O (e.g. HTTP request, a file read, a database query etc.). You don't want to wait with a synchronous while loop, so other functions can be executed in the meantime.

When you get your data, you (permanently) relinquish control and call the callback with the result.

function myFunc(someArg, callback) {     // ...     callback(error, result); } 

Because the callback function may be some code that hasn't been executed yet, and you don't know what's above your function in the call stack, generally instead of throwing errors you pass on the error to the callback as an argument. There are error-first and result-first callback conventions.

Mostly callbacks have been replaced by Promises in the Javascript world and since ES2017+, you can natively use async/await to get rid of callback-rich spaghetti code and make asynchronous control flow look like it was synchronous.

Sometimes, in special cascading control flows you run callbacks in the middle of the function. E.g. in Koa (web server) middleware or Redux middleware you run next() which returns after all the other middlewares in the stack have been run.

Hooks

Hooks are not really a well-defined term, but in Javascript practice, you provide hooks when you want a client (API/library user, child classes etc.) to take optional actions at well-defined points in your control flow.

So a hook may be some function (given as e.g. an argument or a class method) that you call at a certain point e.g. during a database update:

data = beforeUpdate(data);  // ...update  afterUpdate(result); 

Usually the point is that:

  • Hooks can be optional
  • Hooks usually are waited for i.e. they are there to modify some data
  • There is at most one function called per hook (contrary to events)

React makes use of hooks in its Hooks API, and they - quoting their definition - "are functions that let you “hook into” React state and lifecycle features", i.e. they let you change React state and also run custom functions each time when certain parts of the state change.

Events

In Javascript, events are emitted at certain points in time, and clients can subscribe to them. The functions that are called when an event happens are called listeners - or for added confusion, callbacks. I prefer to shun the term "callback" for this, and use the term "listener" instead.

This is also a generic OOP pattern.

In front-end there's a DOM interface for events, in node.js you have the EventEmitter interface. A sophisticated asynchronous version is implemented in ReactiveX.

Properties of events:

  • There may be multiple listeners/callbacks subscribed (to be executed) for the same event.
  • They usually don't receive a callback, only some event information and are run synchronously
  • Generally, and unlike hooks, they are not for modifying data inside the event emitter's control flow. The emitter doesn't care 'if there is anybody listening'. It just calls the listeners with the event data and then continues right away.

Examples: events happen when a data stream starts or ends, a user clicks on a button or modifies an input field.

like image 35
gombosg Avatar answered Oct 12 '22 14:10

gombosg