Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS Async Callback
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);
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);
});
function findUser(id) {
const user = {
id: id,
name: "User" + id,
email: id + "@test.com"
};
return user;
}
const user = findUser(1);
console.log("user:", user);
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