If it's a "cheap" operation, that's perfectly fine. Any operations which are performed multiple times where you're not doing CPU-intensive operations can (and often should) be refactored into a "normal" function. This is fine:
function add(x, y) { return x + y; }
This is not:
function fib(n) {
if (n <= 1) return 1;
// this may take a long time if n is large
return fib(n - 1) + fib (n - 2);
}
A possible implementation of fib would take a function to return the result as a parameter, and spread the calculation over multiple calls. Something like the code below, from the book "Node Web Development":
function fib(n, done) {
if (n <= 1) {
done(1);
} else {
process.nextTick(function() {
fib(n - 1, function(val1) {
process.nextTick(function() {
fib(n - 2, function(val2) {
done(val1 + val2);
});
});
});
});
}
}
Where the thread wouldn't be blocked for long.
Yes, a better alternative would be to implement fibonacci in the iterative way, not the recursive one. But this is just to demonstrate how one could "split" a CPU-heavy function.