I'm a bit of a beginner to JavaScript and I've been trying to figure this out for at least two hours. If someone could explain to me why this is happening, it'll be great!
function slowDouble(x, callback) {
setTimeout(function() {
callback(2 * x);
}, 500);
}
function slowDoubleTenTimes(x, callback) {
for (i = 0; i < 10; i++) {
slowDouble(x, function(result) {
x = result;
});
}
callback(x);
}
slowDoubleTenTimes(3, function(result){
console.log('The result of slowDoubleTenTimes is ' + result);
});
Logic is telling me that in slowDoubleTenTimes
,
in the for loop, x
should be changing.
And every time it calls slowDouble
again in the subsequent for-loop iteration,
x
should be different.
But x
remains at 3
!
In fact, the resulting answer in callback(x)
should be 3072
.
Yet, x
changes from 3
to 6
and then remains at 6
.
Is there something about JavaScript that I don't know that's preventing the result from changing?
Also, the weird thing is, if I put console.log("hi")
after the for-loop,
the console prints out "hi" before slowDouble
runs.
Shouldn't slowDouble
run before console.log("hi")
?
Or is there something about setTimeout
that I'm not understanding correctly?
Thank you!
slowDouble does not block. So, "callback" is invoked right away.
setTimeout says, "run this function in 500 milliseconds". But, it doesn't block, which means that it continues to the next lines after it registered to be invoked in 500 ms.
JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution.
If a timer is blocked from immediately executing it will be delayed
until the next possible point of execution (which will be longer than
the desired delay).
http://ejohn.org/blog/how-javascript-timers-work/
function slowDouble(x, callback) {
setTimeout(function() {
callback(2 * x);
}, 500);
}
function slowDoubleTenTimes(x, callback) {
for (i = 0; i < 10; i++) {
slowDouble(x, function(result) {
x = result;
});
}
callback(x); //This will get called before the callback from timeout
// in slowDouble's context is called.
// console.log(3)
}
slowDoubleTenTimes(3, function(result){
console.log('The result of slowDoubleTenTimes is ' + result);
});
I have the following code: //Marks all users which are reading the book with the bookId var markAsReading = function (bookId,cb) { User.find({}, function (err,users) { if(err) ...
I have the following code: //Marks all users which are reading the book with the bookId var markAsReading = function (bookId,cb) { User.find({}, function (err,users) { if(err) ...
In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...
In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...
I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...
I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...
Well, I have some function that invokes via ng-click. For example, this function set variable a to true, and after few seconds variable should become false. Function looks like this one: $scope.do =...
Well, I have some function that invokes via ng-click. For example, this function set variable a to true, and after few seconds variable should become false. Function looks like this one: $scope.do =...