------"Event snooping" for JavaScript asynchronous programming methods
Another way of thinking is to use event-driven mode. The execution of a task does not depend on the order of the Code, but on whether an event occurs.
Take F1 and F2 as an example. First, bind an event for F1 (the jquery notation used here).
F1.on (' Done ', F2);
The above line of code means that when a done event occurs on F1, the F2 is executed. Then, rewrite the F1:
Function F1 () {setTimeout (function () {///F1 Task Code F1.trigger (' Done ');
}, 1000);
}
F1.trigger (' Done ') indicates that when execution is complete, the Do event is immediately triggered to begin execution of F2.
The advantages of this method are relatively easy to understand, can be bound to multiple events, each event can specify multiple callback functions, and can be "decoupling" (decoupling), is conducive to the implementation of modularity. The downside is that the entire program becomes event-driven, and the running process becomes unclear.
------"Event snooping" for JavaScript asynchronous programming methods