[javascript] Questions about callback

Asked 2 years ago, Updated 2 years ago, 111 views

Hello, the code below is written as an example for callback.

var cbExample = function(number, cb) {
  setTimeout(function() {
    var sum = 0;
    for (var i = number; i > 0; i--) {
      sum += i;
    }
    cb(sum);
  }, 0);
};
cbExample(10, function(result) {
  console.log(result);
}); // 55
console.log('first');

The parameter cb of the cbExample function is the part where the callback function is received, and function(result) { entered as the second factor in the part where cbExample is executed console.log(result); } is registered as a callback function and runs after calculation.

Shouldn't we create and execute the printer function instead of putting the function as a factor as shown below? Normally, the first is executed first and then the calculation result is informed.

var cbExample = function (number) {
    setTimeout(function () {
        var sum = 0;
        for (var i = number; i > 0; i--) {
            sum += i;
        }
        printer(sum);

    }, 0)

}

var printer = function (result) {
    console.log(result)
}

cbExample(10);
console.log('first')

Question 1) Is it necessary to receive a callback function as a factor? I'd like to ask.

Question 2) The callback function has a function called A, which receives a function called B as a factor and executes the function that A originally intended to do, and then acts as B. Should I understand that?

Question 3) Does JavaScript run asynchronously by default? Or do you use callback when it runs synchronously or when it has to run asynchronously?

Question 4) If I'm right about question 2, why is callback used for asynchronous processing? Isn't it just that function A is executed in order, and then function B is executed...?

Thank you for reading my question! :)

javascript callback

2022-09-22 19:20

1 Answers

1) In the above case, the callback function called printer is already directly accessible within the function called cbExample, so it is not necessary to receive it as a parameter. However, if you look at the actual callback, the cbExample function that passes the parameter is difficult to understand, or the function that goes to the callback is often in various forms. In such cases, receiving and using a callback is useful because you can externally take control of code after the cbExample function (or in the execution process).

2) It depends on the implementation of a function that takes over another function (callback). If there is an asynchronous call in function A, B uses a callback in the form of receiving and processing the result, so B is called to the endpoint of function A. But in some cases, we use JavaScript to implement polymorphism. It's also a feature of higher order functions. In this case, it does not necessarily call at the end of the A function, but calls from the intermediate logic to create various mutations. You can move on knowing that there's something like that.

3) JavaScript is basically a synchronization code. There are only a few special cases (setTimeout, Ajax, doc/img loading, etc.) that enter asynchronously. However, if you enter asynchronously, you need a callback to continue the flow of the code.

4) If you don't go deep, you won't understand why. You need to know more about how events in JavaScript work.

First of all, you have to understand roughly what asynchronous is and how it works. Downloading the image is asynchronous in a different area than the workspace running the main code. The main code area is Demonstrate 'Let me know when you're done' to the downloader and execute the main code constantly. If the download is complete while the main code is running, wait in the event queue. The main code builds up on the stack and disappears when it runs, repeating . If it's not an infinite loop, there will eventually be a moment when nothing of the main code is left in the stack. Runs the task in the queue at that time. Here, the action in the queue is the callback function that you handed over to the function that you called to start the download.

The above sequence of processes is why callback is required in asynchronous mode. As I said in No. 3, if you block the main code and wait indefinitely in a single-threaded situation that basically works synchronously, the performance will not be good. In JavaScript, the callback system allows smooth operation of other routines.

Why JavaScript uses events Why did you choose the non-blocking method? Why doesn't it work in the order of the code you write? Why callback is necessary is to further understand the characteristics of the language and how it works.

I hope it helps you I will link the reply I posted before.

JavaScript engine basics, although it is a text about a node The contents of the movement are not that different, so I think you just need to replace the terms and read them. https://hashcode.co.kr/questions/6621/indexjs-%EC%95%88%EC%97%90-%EC%A0%81%EC%9D%80-%EC%BD%94%EB%93%9C%EB%93%A4%EC%9D%80-event-loop%EA%B0%80-%EB%8F%8C%EA%B8%B0-%EC%A0%84%EC%97%90-%EC%8B%A4%ED%96%89%EB%90%A9%EB%8B%88%EA%B9%8C

https://hashcode.co.kr/questions/6658/nodejs-%EC%BD%9C%EB%B0%B1%ED%95%A8%EC%88%98-%EC%A7%88%EB%AC%B8%EC%9E%85%EB%8B%88%EB%8B%A4

There are many good articles on the Internet. Look it up.


2022-09-22 19:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.