Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created October 18, 2018 05:20
Show Gist options
  • Save salmanx/14d8105c367ced1285fdded5abfc9696 to your computer and use it in GitHub Desktop.
Save salmanx/14d8105c367ced1285fdded5abfc9696 to your computer and use it in GitHub Desktop.
an example how javascript callback works
console.log('Before');
getUser(1, displayUser);
console.log("After");
function getUser(id, callback){
setTimeout(() => {
console.log("Reading a user from database!");
callback({ id: id, name: "salmanx" })
}, 2000)
}
function getRepositories(username, callback){
setTimeout(() => {
console.log("Calling github api");
callback(["repo1", "repo2", "repo3"]);
}, 2000)
}
function getCommits(repo, callback){
setTimeout(() => {
callback(["commit1", "commit2"]);
}, 2000)
}
function displayCommits(commits){
console.log(commits);
}
function displayRepos(repos){
getCommits(repos[0], displayCommits);
}
function displayUser(user){
getRepositories(user.name, displayRepos)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment