Skip to content

Commit dd155a5

Browse files
committed
updates to about.md
- added basic example to about - added detail about array prototype function which use callbacks
1 parent 7485701 commit dd155a5

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

concepts/callbacks/about.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
# About
22

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+
```
428

529
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.
630

@@ -52,6 +76,40 @@ operation(1, 2, callback)
5276

5377
You see this pattern often when dealing with asynchronous functions to assist with control flow.
5478

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+
55113
[mdn-callbacks]: https://developer.mozilla.org/en-US/docs/Glossary/Callback_function
56114
[mdn-concurrency-stack]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#stack
57115
[mdn-concurrency-queue]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#queue

concepts/callbacks/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ function squareArea(side) {
1717
return side * side;
1818
}
1919

20-
applyToSquare(areaOfSquare); // => 25
20+
applySideLength(areaOfSquare); // => 25
2121
```
2222

2323
You may also write callbacks as a function expression:
2424

2525
```javascript
26-
applyToSquare(function squarePerimeterLength(side) {
26+
applySideLength(function squarePerimeterLength(side) {
2727
return side * 4;
2828
});
2929
```

0 commit comments

Comments
 (0)