|
1 | 1 | # About
|
2 | 2 |
|
3 |
| -[_Callback_ functions][wiki-callbacks] are functions passed as arguments to other functions. The callback function may then be invoked to create a sequence of events. Often, _callbacks_ are used to handle the results of work done, or handle an action when an event occurs. _Callback_ functions can be used in synchronous and asynchronous programming. |
| 3 | +[_Callback_ functions][wiki-callbacks] are functions passed as arguments to other functions. The callback function may then be invoked to trigger a subsequent action. Often, _callbacks_ are used to handle the results of work done, or handle an action when an event occurs. _Callback_ functions can be used in synchronous and asynchronous programming. |
| 4 | + |
| 5 | +```javascript |
| 6 | +const sideLength = 5; |
| 7 | + |
| 8 | +// Caller function takes a callback function |
| 9 | +function applySideLength(callback) { |
| 10 | + return callback(sideLength); |
| 11 | +} |
| 12 | + |
| 13 | +// Callback must expect the possible argument from the calling function |
| 14 | +function squareArea(side) { |
| 15 | + return side * side; |
| 16 | +} |
| 17 | + |
| 18 | +applySideLength(areaOfSquare); // => 25 |
| 19 | +``` |
| 20 | + |
| 21 | +You may also write callbacks as a function expression: |
| 22 | + |
| 23 | +```javascript |
| 24 | +applySideLength(function squarePerimeter(side) { |
| 25 | + return side * 4; |
| 26 | +}); |
| 27 | +``` |
4 | 28 |
|
5 | 29 | This is a useful pattern in JavaScript because JavaScript is designed as a single-threaded runtime where only one function call can be executed at a time. During execution, the runtime cannot respond to other events or continue execution until the function has returned.
|
6 | 30 |
|
@@ -52,6 +76,40 @@ operation(1, 2, callback)
|
52 | 76 |
|
53 | 77 | You see this pattern often when dealing with asynchronous functions to assist with control flow.
|
54 | 78 |
|
| 79 | +### Callbacks in disguise |
| 80 | + |
| 81 | +Common `Array` functions use callback functions to define their behaviour: |
| 82 | + |
| 83 | +- `Array.prototype.forEach`: |
| 84 | + - Accepts a callback, which applies the callback to each element of an array. |
| 85 | + |
| 86 | + ```javascript |
| 87 | + [1, 2, 3].forEach(function (element) { |
| 88 | + doSomething(element) |
| 89 | + }); |
| 90 | + // => doSomething() is invoked 3 times, once with each element |
| 91 | + ``` |
| 92 | + |
| 93 | +- `Array.prototype.map` |
| 94 | + - Accepts a callback, which applies the callback to each element of an array using the result to create a new array. |
| 95 | + |
| 96 | + ```javascript |
| 97 | + [1,2,3].map(function (element) { |
| 98 | + return element + 1; |
| 99 | + }); |
| 100 | + // => [2, 3, 4] |
| 101 | + ``` |
| 102 | + |
| 103 | +- `Array.prototype.reduce` |
| 104 | + - Accepts a callback, which applies the callback to each element of an array, passing the result forward to the next invocation. |
| 105 | + |
| 106 | + ```javascript |
| 107 | + [1,2,3].reduce(function (runningSum, element) { |
| 108 | + return runningSum + element; |
| 109 | + }, 0); |
| 110 | + // => 6 |
| 111 | + ``` |
| 112 | + |
55 | 113 | [mdn-callbacks]: https://developer.mozilla.org/en-US/docs/Glossary/Callback_function
|
56 | 114 | [mdn-concurrency-stack]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#stack
|
57 | 115 | [mdn-concurrency-queue]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#queue
|
|
0 commit comments