Skip to content

Instantly share code, notes, and snippets.

@MdShohanurRahman
Created December 8, 2020 16:25
Show Gist options
  • Save MdShohanurRahman/eb204f620e38bc4cae89cd65124f2a71 to your computer and use it in GitHub Desktop.
Save MdShohanurRahman/eb204f620e38bc4cae89cd65124f2a71 to your computer and use it in GitHub Desktop.
Callback in Js
getUser(1, user => {
console.log('user', user)
getRepo(user.githubUserName, (repo) => {
console.log('Repos', repo)
})
})
function getUser(id, callback) {
setTimeout(()=>{
console.log('Reading A user From Database')
callback({id: id, githubUserName: 'shohan'})
},2000)
}
function getRepo(userName, callback) {
setTimeout(() => {
console.log("Calling Githum Api")
callback(['repo1', 'repo2', 'repo3']) // we can't return directly here
}, 2000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment