How does the js asynchronous operation callback function control the order of execution

  • 2020-03-30 01:05:16
  • OfStack

Requirements:
Fun A() {asyn(parm1, parm2, onsuccess(){}); }
Fun B() {asyn(paem1, parm2, onsuccess(){}); }

The function B has to be executed after the function A

Asynchronous execution
If used directly
(A);
(B);

The execution conditions cannot be met.

Consider passing B to A as A callback function, and then A executes the B function in onsucess
A (B);

Can achieve functional requirements.

Js is single-threaded.

1. When calling a function, if the number of parameters is more than the number defined, the extra parameters will be ignored; if the number is less than the number defined, the missing parameters will be automatically assigned to undefined values.
2. If the function definition is declared with the function statement, it cannot appear in the loop or conditional statement, but if the function definition is declared with the function direct quantity method, it can appear in any js expression.
3. Arguments object
The arguments object of the function is like an array, which holds the actual arguments when the function is called. You can use arguments[0], arguments[1], arguments[2]... To refer to these parameters, even if they are not present when the function is defined. But arguments are not really array objects.
Function a (x, y) {
Arguments [0] // represents the first argument, x
Arguments [1] // represents the first argument y
Arguments [2] // represents a third argument, provided that three arguments are passed when the function is called
...
Arguments. Length // indicates the number of arguments actually passed
Arguments. Callee (x,y) // call itself}
The arguments object has a length property that represents the number of arguments actually passed when the function is called.
The arguments object also has the callee property to refer to the currently executing function, which is especially useful in anonymous functions.
4. The length property of the function (yes, the function also has the length property)
Unlike arguments.length, the length property of a function represents the number of parameters when the function is defined, not the actual number of arguments when the function is called. The length property of a function can be called with arguments.calle.length.

Related articles: