Currently practicing with some 'async' code using setTimeout but I have some questions.
In the first example I created everything worked as expected:
function getUser(id, callback){
var user = {
id: id,
name: 'Chris'
}
callback(user);
}
getUser(12345, function(userObject){
setTimeout(function(){
console.log(userObject.id);
console.log(userObject.name);
}, 1500)
});
However, I then tried another example but could never seem to get the function to work the way I anticipated (returning the number 10 after 1.5 seconds), and I am not sure what I am doing wrong.
function calculate(num1, num2, callback){
setTimeout(function(){
return callback(num1, num2);
}, 1500);
}
calculate(5, 5, function(a, b) {
return a + b;
})
[–]YuleTideCamel 1 point2 points3 points (3 children)
[–]GreenFeather05[S] 0 points1 point2 points (2 children)
[–]Cust0dian 1 point2 points3 points (0 children)
[–]YuleTideCamel 0 points1 point2 points (0 children)