Skip to content

Instantly share code, notes, and snippets.

@alloy-d
Created September 8, 2012 22:17
Show Gist options
  • Save alloy-d/3680304 to your computer and use it in GitHub Desktop.
Save alloy-d/3680304 to your computer and use it in GitHub Desktop.
Ridiculous callback example
// We're coding an awesome website.
//
// For a great user experience, we want to add a personalized greeting.
// Here is a function that gives said personalized greeting, given the user's name.
function greetUserLikeIts1995(firstName) {
alert("Hello there, " + firstName + "!");
}
// For security, we use carrier pigeons to get information from our users.
// Unfortunately, it's a slow process, so we don't want to just sit around
// waiting for the pigeon-based transaction to complete.
//
// getFirstName takes a user's address and a function to call once our pigeon
// has flown there and back.
function getFirstName(userAddress, callback) {
var firstName = getUserNameByWayOfCarrierPigeon(userAddress);
callback(firstName);
}
// To accomplish our greeting, we'll pass in our greeting function to
// getFirstName. It will send out a pigeon, get the user's first name, and
// then call greetUserLikeIts1995 with the name as an argument.
getFirstName("4201 31st Street S", greetUserLikeIts1995);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment