Created
October 18, 2018 05:20
-
-
Save salmanx/14d8105c367ced1285fdded5abfc9696 to your computer and use it in GitHub Desktop.
an example how javascript callback works
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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