JavaScript Asynchronous Programming method------"Publish/Subscribe"
we assume that there is a "signal center" in which a task executes, a signal is "released" (publish) to the signal center, and other tasks can "subscribe" to the Signal Center (subscribe) to know when they can start executing. This is called the "Publish/Subscribe Mode" (Publish-subscribe pattern), also known as the "Observer Mode" (Observer pattern).
There are several implementations of this pattern, with Ben Alman's tiny Pub/sub, which is a plugin for jquery.
First, F2 subscribes to "done" signals to the signal center jquery.
jquery.subscribe ("Done", F2);
The F1 then overwrites the following:
function F1 () {setTimeout (function () {///F1 Task code jquery.publish ("Done");
}, +);
}
jquery.publish ("done") means that after the completion of the F1 execution, the "Do" signal is released to the signal center jQuery, triggering the execution of the F2.
In addition, F2 can also unsubscribe (unsubscribe) Once the execution is complete.
jquery.unsubscribe ("Done", F2);
This method is similar in nature to "event monitoring", but is significantly better than the latter. Because we can monitor the operation of the program by looking at the message center to see how many signals exist, how many subscribers each signal has.
JavaScript Asynchronous Programming method------"Publish/Subscribe"