Javascript callbacks

What is a callback? A callback is a function to be executed after another function is executed. Sounds tongue-twisted? Normally if you want to call function do_b after function do_a the code looks something like

function do_a(){
  console.log( '`do_a`: this comes out first');
}

function do_b(){
  console.log( '`do_b`: this comes out later' );
}

do_a();
do_b();
Result
`do_a`: this comes out first
`do_b`: this comes out later

However javascript is an event driven language. If do_a takes longer than do_b, the result of do_b comes out first than do_a;

function do_a(){
  // simulate a time consuming function
  setTimeout( function(){
    console.log( '`do_a`: this takes longer than `do_b`' );
  }, 1000 );
}

function do_b(){
  console.log( '`do_b`: this is supposed to come out after `do_a` but it comes out before `do_a`' );
}

do_a();
do_b();
Result
`do_b`: this is supposed to come out after `do_a` but it comes out before `do_a`
`do_a`: this takes longer than `do_b`

So how do we make sure do_b comes out after do_a in that situation? This is where callbacks comes in handy.

function do_a( callback ){
  setTimeout( function(){
    // simulate a time consuming function
    console.log( '`do_a`: this takes longer than `do_b`' );

    // if callback exist execute it
    callback && callback();
  }, 3000 );
}

function do_b(){
  console.log( '`do_b`: now we can make sure `do_b` comes out after `do_a`' );
}

do_a( function(){
  do_b();
});
Result
`do_a`: this takes longer than `do_b`
`do_b`: now we can make sure `do_b` comes out after `do_a`
Different ways of applying a callback
function basic( callback ){
  console.log( 'do something here' );

  var result = 'i am the result of `do something` to be past to the callback';

  // if callback exist execute it
  callback && callback( result );
}

function callbacks_with_call( arg1, arg2, callback ){
  console.log( 'do something here' );

  var result1 = arg1.replace( 'argument', 'result' ),
      result2 = arg2.replace( 'argument', 'result' );

  this.data = 'i am some data that can be use for the callback function with `this` key word';

  // if callback exist execute it
  callback && callback.call( this, result1, result2 );
}

// this is similar to `callbacks_with_call`
// the only difference is we use `apply` instead of `call`
// so we need to pass arguments as an array
function callbacks_with_apply( arg1, arg2, callback ){
  console.log( 'do something here' );

  var result1 = arg1.replace( 'argument', 'result' ),
      result2 = arg2.replace( 'argument', 'result' );

  this.data = 'i am some data that can be use for the callback function with `this` key word';

  // if callback exist execute it
  callback && callback.apply( this, [ result1, result2 ]);
}

basic( function( result ){
  console.log( 'this callback is going to print out the result from the function `basic`' );
  console.log( result );
});

console.log( '--------------------------------------------------------------------------------------' );

( function(){
  var arg1 = 'i am argument1',
      arg2 = 'i am argument2';

  callbacks_with_call( arg1, arg2, function( result1, result2 ){
    console.log( 'this callback is going to print out the results from the function `callbacks_with_call`' );
    console.log( 'result1: ' + result1 );
    console.log( 'result2: ' + result2 );
    console.log( 'data from `callbacks_with_call`: ' + this.data );
  });
})();

console.log( '--------------------------------------------------------------------------------------' );

( function(){
  var arg1 = 'i am argument1',
      arg2 = 'i am argument2';

  callbacks_with_apply( arg1, arg2, function( result1, result2 ){
    console.log( 'this callback is going to print out the result from the function `callbacks_with_apply`' );
    console.log( 'result1: ' + result1 );
    console.log( 'result2: ' + result2 );
    console.log( 'data from `callbacks_with_apply`: ' + this.data );
  });
})();
Result
do something here
this callback is going to print out the result from the function `basic`
i am the result of `do something` to be past to the callback
--------------------------------------------------------------------------------------
do something here
this callback is going to print out the results from the function `callbacks_with_call`
result1: i am result1
result2: i am result2
data from `callbacks_with_call`: i am some data that can be use for the callback function with `this` key word
--------------------------------------------------------------------------------------
do something here
this callback is going to print out the result from the function `callbacks_with_apply`
result1: i am result1
result2: i am result2
data from `callbacks_with_apply`: i am some data that can be use for the callback function with `this` key word

Now we know how to execute node.js code, using require and exports, the importance of function scopes and closures and callbacks. Next we are going to take a look at the most important thing in node.js – Events.


Related posts