Skip to content

Instantly share code, notes, and snippets.

@DaleSeo
Created December 7, 2018 19:49
Show Gist options
  • Save DaleSeo/54793d35ef0196b59d32c2f3c0fb2cf3 to your computer and use it in GitHub Desktop.
Save DaleSeo/54793d35ef0196b59d32c2f3c0fb2cf3 to your computer and use it in GitHub Desktop.
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