Callbacks
Callback functions are functions passed as arguments. This programming pattern creates a sequence of function calls in both synchronous and asynchronous programming. Writing a callback function is no different from writing a function; however, the callback function must match the signature defined by the calling function.
const sideLength = 5;
// Caller function takes a callback function
function applySideLength(callback) {
return callback(sideLength);
}
// Callback must expect the possible argument from the calling function
function areaOfSquare(side) {
return side * side;
}
applySideLength(areaOfSquare); // => 25
You may also write callbacks as a function expression:
applySideLength(function squarePerimeterLength(side) {
return side * 4;
});
References
- http://callbackhell.com/
- https://javascript.info/callbacks
- https://exercism.org/tracks/javascript/concepts/callbacks
- https://www.educative.io/answers/what-are-callbacks-in-javascript
- What the heck is the event loop anyway? | Philip Roberts | JSConf EU (Private)
- https://developer.mozilla.org/en-US/docs/Glossary/Callback_function
- https://nodejs.org/en/knowledge/errors/what-are-the-error-conventions/
- https://www.twilio.com/blog/asynchronous-javascript-understanding-callbacks
- https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/