Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  57] [ 6]  / answers: 1 / hits: 61837  / 14 Years ago, sat, january 8, 2011, 12:00:00

Yes, I know - that question has thousands of answers. please, don't tell me about setTimeout method because - yes, everything is possible with that but not so easy as using sleep() method.



For example:



function fibonacci(n) {
console.log(Computing Fibonacci for + n + ...);
var result = 0;

//wait 1 second before computing for lower n
sleep(1000);
result = (n <= 1) ? 1 : (fibonacci(n - 1) + fibonacci(n - 2));

//wait 1 second before announcing the result
sleep(1000);
console.log(F( + n + ) = + result);

return result;
}


if you know how to get the same result using setTimeout - tell me ;) fibanacci is pretty easy task, because there aren't more than 2 recursions, but how about n-recursions (like fib(1) + fib(2) + .. + fib(n)) and sleep after every +? Nah, sleep would be muuuuuch easier.



But still I can't get working example of implementing it. while (curr - start < time) { curr = (...) } is tricky, but it won't work (just stops my browser and then throw all console logs at once).


More From » sleep

 Answers
2

I dont fully understand what you're asking, but I'm going to answer this part:




if you know how to get the same result
using setTimeout - tell me




The fundamental difference is that sleep (as used in many other languages) is synchronous, while setTimeout (and many other JavaScript-concepts, like AJAX for example) are asynchronous. So, in order to rewrite your function we have to take this into account. Mainly, we have to use a callback to fetch the return value, rather than an actual return-statement, so it will be used like this:



fibonacci(7, function(result) {
// use the result here..
});


So, as for the implementation:



function fibonacci(n, callback) {
console.log(Computing Fibonacci for + n + ...);
var result = 0;

var announceAndReturn = function() {
setTimeout(function() {
// wait 1 second before announcing the result
console.log(F( + n + ) = + result);
callback(result); // returns the value
}, 1000);
};

// wait 1 second before computing lower n
setTimeout(function() {
if (n <= 1) {
result = 1;
announceAndReturn();
}
else {
var resultsLeft = 2;

var handler = function(returned) {
result += returned;
resultsLeft--;
if (resultLeft == 0)
announceAndReturn();
}

fibonacci(n-1, handler);
fibonacci(n-2, handler);
}
}, 1000);
}


I would also like to point out that, no, this is not an easier solution than using sleep. Why? Because this code is asynchronous and that's simply more complicated than synchronous code for what most people are used to. It takes practice to start thinking in that way.



The upside? It allows you to write non-blocking algorithms that outperforms their synchronous counterparts. If you haven't heard of Node.js before, you could check it out to further understand the benefits of this. (Many other languages have libraries for dealing with async IO as well, but as long as were talking about JavaScript..)


[#94321] Thursday, January 6, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cadendericki

Total Points: 482
Total Questions: 109
Total Answers: 103

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
cadendericki questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Wed, Jul 8, 20, 00:00, 4 Years ago
Thu, May 14, 20, 00:00, 4 Years ago
;