Async code

    3s seconds in the future the timeout is done and is echoed to the screen. We can see that the anonymous function we provide is being triggered when time has passed. Now for another more revealing example:

    1. doWork( () => {
    2. console.log('call me when done');
    3. })
    1. function doWork(cb){
    2. setTimeout( () => {
    3. cb();
    4. }

    Other example of callback code are events here demonstrated by a example

    The gist of callbacks and async in general is that one or more methods are invoked sometime in the future, unknown when.

    One word Readability

    Imagine doing the following code

    1. syncCode() // emit 1
    2. syncCode2() // emit 2
    3. asyncCode() // emit 3
    4. syncCode4() // emit 4

    The output could very well be

    1. 1,2,4,3

    We might resort to a callback making it look like

    At this point it is readable, somewhat but imagine we have only async code then it might look like:

    1. asyncCode(() => {
    2. asyncCode3() => {
    3. }
    4. })
    5. })

    Also known as callback hell, pause for effect :)

    1. getData()
    2. .then(getMoreData)

    This is great for Request/Response patterns but for more advanced async scenarios I dare say only Rxjs fits the bill.