Skip to content

Instantly share code, notes, and snippets.

@nvjkmr
Forked from joepie91/.js
Last active January 29, 2017 14:57
Show Gist options
  • Save nvjkmr/9a3c0a7afc63917c3d4cbb98ff53362a to your computer and use it in GitHub Desktop.
Save nvjkmr/9a3c0a7afc63917c3d4cbb98ff53362a to your computer and use it in GitHub Desktop.
Callbacks in Javascript
function getUser(userID) {
/* This is a hypothetical function. Normally we'd talk to a database or so, but this is to illustrate the concept... */
var someUser = {name: "Joe Bloggs", age: 42};
cb(someUser); /* Here we call the callback from the second function argument, with our fake user */
}
var cb = function(user){
/* Now `user` contains {name: "Joe Bloggs", age: 42} */
}
/* Code: */
getUser(42);
/* We could also write this as: */
// function whenDone(user) {
/* Now `user` contains {name: "Joe Bloggs", age: 42} */
// }
//getUser(42, whenDone);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment