Exploring Callbacks
Open this page in jsbin. All the interesting things are in the JavaScript panel and the Console.
Several interesting things are happening here:
- Announce that we're starting the sequence
- Call a function: doSomething () that has a callback function. The callback will take 1500 milliseconds.
- Announce that we're waiting for doSomething() to happen
- Carry on with several other operations¹ even though doSomething() is still running (has not finished but the processing carries on asynchronously.
- Eventually doSomething() completes and its callback let's us now that it's done.
¹ Other operations demonstrating that we can pass a function as a parameter to another function.
- function doMath( number, number, function)
- We have two more functions:
- subtract ( number, number )
- multiply ( number, number )
- We call doMath() three times
- doMath ( number, number , subtract)
- doMath ( number, number , multiply)
- doMath ( number, number , function(){})