Leo
Leo

Reputation: 2103

are 'callbacks' just a naming convention?

i've read that callbacks are functions triggered after the rest of the logic is done like this:

function withCallback(argument, callback) {
  doSomethingWithParams(argument);
  callback():
}

but as far as i can tell these callbacks need to be declared all the same, there is no universal method of putting callbacks wherever you want every time? i guess my question is: are callbacks special in a technical way, or is it just a conventional name for a function declared and executed at the end of another function?

Upvotes: 3

Views: 1955

Answers (3)

unglued
unglued

Reputation: 1051

To make it a little bit clear and readable, instead of callback you can use name next.

Upvotes: 0

Terry Lennox
Terry Lennox

Reputation: 30675

Callbacks are just functions and they're named very appropriately. They are "called back" when an operation is complete, when a resource is ready etc. Callbacks are not unique to JavaScript, they've been around for decades. Anytime a caller wants to make an asynchronous call and receive the result at a later date, this pattern makes sense.

With regard to standardization, there is no standard way of defining how a callback should look, however you'll find that they are similar, for example, Node.js uses an error first callback signature, passing any error as the first argument to the callback function.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074188

There's nothing special about callbacks; they're just functions (in JavaScript). The term, "callback," is used to indicate that the function provided by the caller is called back at some appropriate time as defined by whatever is accepting the callback. Even then, we don't always use the term "callback." Event handlers are callbacks, for instance, but we hardly ever call them that.

...as far as i can tell these callbacks need to be declared all the same...

Not at all. The onreadystatechange callback for XMLHttpRequest doesn't receive any arguments, but the Array#sort callback receives two, while Array#forEach receives three (though often people only use one or two of them). They're all different. Nor do all callbacks have to be defined inline, and often, they aren't. So they aren't all declared the same way.

There are three common "standard" forms of callbacks that I know of, though:

  1. DOM event handlers: When called, they receive an event object as their first argument, and in the normal case, this will be set to the element on which the handler was registered. Its return value is ignored in the standard form (addEventListener) but used for some things by others (see my post: The true story of return false).

  2. Promise callbacks, which come in two forms:

    1. The promise executor, which is the function you pass new Promise, which receives two arguments: The function to use to resolve the project, and the function to use to reject it.

    2. The then and catch callback, which receives (in a standard implementation) a single argument which is the resolution value or error that occurred. The return value of the callback settles the promise created by the call to then or catch, and throwing an error from either of them rejects that promise.

  3. Node.js-style API callbacks: These receive an error or null as their first argument, followed by zero or more non-error values if null is passed as the first argument.

There are probably others.

Upvotes: 4

Related Questions