JavaScript Asynchronous Programming method------"Publish/Subscribe"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.