[RxJS] Implement pause and resume feature correctly through RxJS

Source: Internet
Author: User

Eventually you'll feel the need for pausing the observation of a Observable and resuming it later. In this lesson we'll learn about the use cases where pausing are possible, and what does when pausing is impossible.

Constresume$ =NewRx.subject ();Constres$ =resume$. Switchmap (Resume=Resume?Rx.Observable.interval ( -): Rx.Observable.empty ()). Do(x = Console.log ('Request It!'+x). Switchmap (EV=Rx.Observable.ajax ({URL:'HTTPS://JSONPLACEHOLDER.TYPICODE.COM/USERS/1', Method:'GET',  })); Res$.subscribe (function (data) {Console.log (data.response);}); Resume$.next (false); SetTimeout (()= Resume$.next (true), -); SetTimeout (()= Resume$.next (false), the);

Here use

Rx.Observable.empty ()

Inside Switchmap (), it means if code goes to empty () and then the rest of code:

.  Do (). Switchmap ()

Won ' t run.

If just subscribe, it trigger complete function:

var source = Rx.Observable.empty (); var subscription = Source.subscribe (  function (x) {    console.log ('Next:%s  ', x);  },  function (err) {    console.log ('Error:%s  ', err);  },  function () {    console.log ('completed' );  });   // = Completed

[RxJS] Implement pause and resume feature correctly through RxJS

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.