Implementing a Callback in Javascript

Last modified: 
Friday, April 24th, 2015
Topics: 
Javascript

This is an example of implementing a callback in Javascript.

function callMeBack(message, callback) {
    if (typeof message == "string") {
        console.log(message);
    }
    else {
        throw "A message argument in the form of a string is required.";
    }

    if (callback && typeof callback == "function") {
        callback();
    }
}

message = "The callMeBack function is being called without a callback argument";
callMeBack(message);


message = "This callMeBack function is now called with a callback.";
callMeBack(message, function(){
    console.log("This was passed as anonymous function to the callback argument.");
});

Callbacks within functions implementing callbacks

If you're implementing a callback in a function a() which contains calls to a function b(), where b() itself implements it's own callback, it is possible for a() to complete before b() completes, unless the callback implementation for a() is within the callback argument passed to b(). See demonstration below.

var http = require("http");

function tricksyOne(callback) {

    console.log("tricksyOne, GO!");

    var host = "trafficjamapp.herokuapp.com";
    http.get({host:host, path:"/?delay=2000"},
        function(res){
            console.log("Data returned from " + host + " on tricksyOne");
        }
    ).on("error", function(e){
            console.log("There was an error "+ e);
        }
    );

    /*
     * Implementing our function's callback here allows tricksyOne() to
     * complete before our http.get() call completes.
     */
    if (typeof callback == "function") {
        callback();
    }
}


function tricksyTwo(callback) {

    console.log("tricksyTwo, GO!");

    var host = "trafficjamapp.herokuapp.com";
    http.get({host:host, path:"/?delay=2000"},
        function(res) {
            console.log("Data returned from " + host + " on tricksyTwo");
            /*
             * Here have implemented tricksyTwo's callback within callback
             * argument passed to http.get(), so it won't fire unless and
             * until tricksyTwo completes.
             */
            if (typeof callback == "function") {
                callback();
            }
        }
    ).on("error", function(e){
            console.log("There was an error "+ e);
        }
    );
}

tricksyOne(function() {
    console.log("My callback on tricksyOne fires BEFORE http.get returns.");
});

tricksyTwo(function() {
    console.log("My callback on tricksyTwo fires AFTER http.get returns.");
});

Running this in NodeJS, we get the following...

$ node blocking.js 
tricksyOne, GO!
My callback on tricksyOne fires BEFORE http.get returns.
tricksyTwo, GO!
Data returned from trafficjamapp.herokuapp.com on tricksyOne
Data returned from trafficjamapp.herokuapp.com on tricksyTwo
My callback on tricksyTwo fires AFTER http.get returns.


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.