1/*
2tl;dr: Callbacks are a way to force one function to complete,
3before running another. Used for asynchronus programming.
4*/
5
6// Consider this example:
7
8names = ['Anthony', 'Betty', 'Camie']
9
10function addName(name, callback){
11 setTimeout(function(){
12 names.append(name)
13 callback()
14 }, 200)
15}
16
17function getNames(){
18 setTimeout(function(){
19 console.log(names)
20 }, 100)
21}
22
23addName('Daniel', getNames)
24/*
25addName('Daniel', getNames) is saying:
26'finish the function addName() BEFORE running the function getNames()'
27
28> 'getNames()' here is the callback
29
30Without the call back in this example, getNames would finish
31before addName. (setTimeout is used to force one function to go slower)
32
33Callbacks are given as arguments to other functions
34
35*/