1.1 callback function

introduction:

  When we come into contact with a new knowledge, generally encounter three questions: What is? What is the use (when to use, used to do)? How to use?

  These three issues, which is a problem we have to learn new knowledge to be solved.

 

First, what is a callback function?

The so-called callback function is: your definition, you do not call, but in the end it carried out, such a function is a callback function;

Such as:

// synchronization callbacks 
the let ARR = [ 'A', 'B', 'C' ]
 // forEach (callbackfn: (value: T, index: Number, Array: T []) => void, the any thisArg ?:) : void; 
arr.forEach ((value) => { 
    the console.log (value); // A, B, C 
}) 

// asynchronous callback 
$ .get (url, callbackfn (data ))

When the loop through the array, forEach (callbackfn) requires that we pass a callback function, I passed here is a function of the arrow (anonymous function), this function

Is my definition, I did not call, but it eventually executed.

 

Second, the callback function what is the use?

  The callback function with synchronous, asynchronous and there is no direct link, the callback is only one implementation, both synchronous callbacks, they can have an asynchronous callback,
may also be delayed and event handling callback function callbacks, which have a lot in our work scenes to be used.

  So, in fact, not that we do not know the callback function, but we linger on in the "callback" of the word, when you see a function
it will be confused, in fact, it's just a parameter name only, just but this argument is a function. And here forEash higher order function is a function of
the number (as a function of the received parameters or a function referred to as a higher order function of the output function returns).

 

Why use a callback function?

(1) with respect to the callback function and js js single-threaded and asynchronous mechanism

  Because js is single-threaded, this design pattern gives us a lot of convenient place, we need to consider the communication between the various threads, do not need to write a lot of code to burn the brain,
that is to say the engine only js able to accomplish one thing and the implementation of related operations, so all the things you need to do is like the same queue, waiting to be triggered and executed, but if this
kind of case, if there is one thing in the queue takes a lot of time, then the back mission will be in a wait state, and sometimes there will be a browser suspended animation phenomenon, for example, that
there is a task one is doing is an infinite loop (longer calls time), then the other will lead to follow-up the task can not be performed properly, so js design flaw in the synchronization mechanism of
the asynchronous mode.
  In asynchronous execution mode, each asynchronous task has its own or with a more callback functions, so that after the current asynchronous tasks have been performed, do not execute immediately
the next task in the queue pieces, and it is the implementation of a callback function, but also will not wait for the next task is currently executing the callback function, because it can not determine the current pullback proper execution
is completed, as long as it is triggered will lead the implementation.

 

 Multithreading (2) js single-threaded browser kernel

The browser kernel is multi-threaded, as shown: cayley sketch

 

Three permanent thread of the browser: js engine thread, GUI rendering thread, browser events to trigger thread

Browser is a multi-threaded execution environment, multiple threads allocated in the browser kernel, one of the most important thread that is js engine thread, while asynchronous requests js event queue,

Interactive event triggers, timers and other events are triggered by the browser thread monitor events after the browser triggers the event thread is triggered task will be added to the task queue js engine

When js engine idle time will begin to perform the task.

 

Reference documents:

About js callback function callback, easy to understand

Understand javascript callbacks

Guess you like

Origin www.cnblogs.com/shiyun32/p/11294450.html