Created
December 7, 2018 19:49
-
-
Save DaleSeo/54793d35ef0196b59d32c2f3c0fb2cf3 to your computer and use it in GitHub Desktop.
JS Async Callback
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
function findUser(id) { | |
let user; | |
setTimeout(function() { | |
console.log("waited 0.1 sec."); | |
user = { | |
id: id, | |
name: "User" + id, | |
email: id + "@test.com" | |
}; | |
}, 100); | |
return user; | |
} | |
const user = findUser(1); | |
console.log("user:", user); |
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
function findUserAndCallBack(id, cb) { | |
setTimeout(function() { | |
console.log("waited 0.1 sec."); | |
const user = { | |
id: id, | |
name: "User" + id, | |
email: id + "@test.com" | |
}; | |
cb(user); | |
}, 100); | |
} | |
findUserAndCallBack(1, function(user) { | |
console.log("user:", user); | |
}); |
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
function findUser(id) { | |
const user = { | |
id: id, | |
name: "User" + id, | |
email: id + "@test.com" | |
}; | |
return user; | |
} | |
const user = findUser(1); | |
console.log("user:", user); |
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
function findUserAndCallBack(id, cb) { | |
const user = { | |
id: id, | |
name: "User" + id, | |
email: id + "@test.com" | |
}; | |
cb(user); | |
} | |
findUserAndCallBack(1, function(user) { | |
console.log("user:", user); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment