I'm brand new to node and trying to wrap my head around callbacks and how to use them. If i'm understanding correct, you can use callbacks as a way to unblock your code so that functions that may take a while to execute won't get in the way of anything that comes after. I'm trying an example that I made up on my own and can't get it to work right.
In this example, the recursive function will take a few seconds to complete. How would you write the callback so that the system console logs 1, then 2, then the answer to the cursive function to show that the recursive function isn't blocking anything afterward?
function recursive(n) {
if(n <= 2) {
return 1;
} else {
return recursive(n - 1) + recursive(n - 2);
}
};
console.log(1);
console.log(recursive(42))
console.log(2);