Created
December 8, 2020 16:25
-
-
Save MdShohanurRahman/eb204f620e38bc4cae89cd65124f2a71 to your computer and use it in GitHub Desktop.
Callback in Js
This file contains 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
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