var fibonacci = exports.fibonacci = function(n) {
if (n === 1)
return 1;
else if (n === 2)
return 1;
else
return fibonacci(n-1) + fibonacci(n-2);
}
var fibonacciAsync = exports.fibonacciAsync = function(n, done) {
if (n === 1 || n === 2)
done(1);
else {
process.nextTick(function() {
fibonacciAsync(n-1, function(val1) {
process.nextTick(function() {
fibonacciAsync(n-2, function(val2) {
done(val1+val2);
});
});
});
});
}
}
其实后者实现的异步函数,也就是把运算的时间延长了是吧? 只是让node在每次事件循环中只运行一次加法,而保证node可以及时响应请求是不是?