Skip to content

Instantly share code, notes, and snippets.

@dhruvdutt
Last active January 25, 2016 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhruvdutt/ffa268bd8b7bd5e6fdef to your computer and use it in GitHub Desktop.
Save dhruvdutt/ffa268bd8b7bd5e6fdef to your computer and use it in GitHub Desktop.
Basic Callbacks https://repl.it/Bggq/0
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 );
}
basic( function( result ){
console.log( 'this callback is going to print out the result from the function `basic`' );
console.log( 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 ]);
}
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 );
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment