Hello.
I have a question, so I'm posting it like this.
I'll explain it with the code first.
function a(){ // Function that takes 100-150 ms to spin once. time-consuming at for
for(){
// Calculation, calculation, calculation...
if (meets conditions during calculation) {
Call b(); // func b()
};
};
};
function b(){
c(()=>{
d(()=>{
console.log ("Arrival!");
});
};
};
function c(callback){
callback(null);
};
function d(callback){
callback(null);
};
// // Run
setInterval(()=>{
Start by calling a(); // funca()
}, 200);
I made a program like the above, and I'm posting a question because I have some questions.
Q. Questions
If both of the above are incorrect, I would like to know how to stack b(), c(), and d() on the top of the call stack.
The problem I have now is that there is a difference in the speed between calling b() individually and running a() execution as shown above.
The reason why I wrote the p.s. code as above is that there is a part that needs to be processed by receiving a response from callback.
node.js stack
1.
After 200 ms, a runs and a builds up in the call stack. If the conditions are met (except for anonymous functions), it is correct that b, c, and d are additionally stacked on the call stack.
However, if the execution time of the function a is greater than 200 ms specified by interval, It only runs at the earliest timing when the call stack is empty (previously b, c, d are all finished, at the end of the a function), but it does not execute the next a function call by overlapping exactly at 200 ms. This is due to the fact that there is only one thread running the main code.
As I mentioned in the comment above, if the execution time of function a exceeds 200 ms, the cycle may not be 200 ms at that time.
2.
SetInterval will run only one function at a time due to the nature of the function implementation. In other words, even if the time expires before the previous call ends, the next call does not occur. Therefore, in the logic above, you don't have to worry about the accumulation of function calls somewhere and slowing them down. If you were in that situation in the first place, you would have seen an error (maximum call stack size exceeded) that the call stack was overflowing.
Anyway, the code never runs in parallel. Functions a are stacked in the call stack and functions called from within are stacked sequentially. And then it's emptied in reverse order. The next a function call is stacked in the call stack only after all is emptied. The next batch of b,c,d interrupts the previous run of a function and cannot be executed first because this series of function calls operates as a single bundle.
If any of the functions b, c, or d has an async function, the story could be a little different. If the cycle of more than 200 ms continues or the asynchronous callback is called late, the call is driven at a certain pointA situation in which the callback is executed regardless of the order of the call... It's going to get more complicated. (I don't even want to think about this. h)
© 2024 OneMinuteCode. All rights reserved.