Skip to content

Instantly share code, notes, and snippets.

@Sysetup
Created October 10, 2016 23:44
Show Gist options
  • Save Sysetup/7cfd8b018175d8d09c7d61d070984f19 to your computer and use it in GitHub Desktop.
Save Sysetup/7cfd8b018175d8d09c7d61d070984f19 to your computer and use it in GitHub Desktop.
Basic callback examples
var l = 5,
b = 5;
function sayHello(greeting){
greeting(function(){
return 'Hello world'
});
}
function area1(x,y,callback){
callback(function(){
return (x*y);
});
}
function area2(x,y,callback){
callback(
{
area:function(){
return (x*y);
}
}
)
}
function area3(x,y,callback){
callback(
function(){
console.log ('Ready: ' + x*y);
}
)
}
sayHello(function (greeting) {
console.log(greeting());
});
area1(l,b,function(returned){
console.log('Ready: '+ returned());
});
area2(l,b,function(returned){
console.log('Ready: '+ returned.area());
});
area3(l,b,function(returned){
returned();
});
//Callback: Calling a function having as parameter other function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment