[RXJS] Creating an Observable with RxJS

Source: Internet
Author: User
Tags try catch

Create an observable

varObservable =rx.observable;varSource = Observable.create (function(observe) {varperson ={name:"Zhentian", message:"Hello world!"    };    Observe.onnext (person); Observe.oncompleted ();});varSub = Source.subscribe (functionOnNext (person) {Console.log (Person.name+ ' say ' +person.message);},functionOnError (Err) {Console.log (err);},functiononcompleted () {Console.log ("Done");});//Zhentian say Hello world!// Done

Async

varObservable =rx.observable;varSource = Observable.create (function(Observe) {SetTimeout (function(){        varperson ={name:"Zhentian", message:"Hello world!"    };    Observe.onnext (person);  Observe.oncompleted (); }, 1000); Console.log ("Ansyc finished!");});varSub = Source.subscribe (functionOnNext (person) {Console.log (Person.name+ ' say ' +person.message);},functionOnError (Err) {Console.log (err);},functiononcompleted () {Console.log ("Done");});//"Ansyc finished!"//"Zhentian say Hello world!"//"Done"

Dispose the Async

When you dispose of the operation, we can see it logs out "start timeout", which are not good, because, the OnNext () would neve R be called, what we want is it even don ' t get inside setTimeout function.

varObservable =rx.observable;varSource = Observable.create (function(Observe) {SetTimeout (function() {Console.log ("Starat Timeout"); varperson ={name:"Zhentian", message:"Hello world!"    };    Observe.onnext (person);  Observe.oncompleted (); }, 1000); Console.log ("Ansyc finished!");});varSub = Source.subscribe (functionOnNext (person) {Console.log (Person.name+ ' say ' +person.message);},functionOnError (Err) {Console.log (err);},functiononcompleted () {Console.log ("Done");}); SetTimeout (function() {sub.dispose ();},500);/*" ansyc finished!" " Starat Timeout "*/

Define the Dispose

We can give setTimeout and ID, and in the return function, we clear this timeout.

varObservable =rx.observable;varSource = Observable.create (function(observe) {varid = setTimeout (function() {Console.log ("Starat Timeout"); varperson ={name:"Zhentian", message:"Hello world!"    };    Observe.onnext (person);  Observe.oncompleted (); }, 1000); Console.log ("Ansyc finished!"); //Note that this is optional, and you does not have the to return this if you require no cleanup  return function() {cleartimeout (ID); }});varSub = Source.subscribe (functionOnNext (person) {Console.log (Person.name+ ' say ' +person.message);},functionOnError (Err) {Console.log (err);},functiononcompleted () {Console.log ("Done");}); SetTimeout (function() {sub.dispose ();},500);/*"Ansyc finished!"*/

Catch Error

If we throw an error with the code, but we found it actually not catched by the OnError handler.

varObservable =rx.observable;varSource = Observable.create (function(observe) {varid = setTimeout (function(){    Throw"There is an error";//Throw an error here    varperson ={name:"Zhentian", message:"Hello world!"    };    Observe.onnext (person);  Observe.oncompleted (); }, 1000); //Note that this is optional, and you does not have the to return this if you require no cleanup  return function() {cleartimeout (ID); }});varSub = Source.subscribe (functionOnNext (person) {Console.log (Person.name+ ' say ' +person.message);},functionOnError (Err) {Console.log ("Error:" +err);},functiononcompleted () {Console.log ("Done");});/*"Error" "Uncaught there is a error (line 6)"*/

What we can does is to add a try catch in the block.

varObservable =rx.observable;varSource = Observable.create (function(observe) {varid = setTimeout (function(){    Try{       Throw"There is an error";//Throw an error here       varperson ={name:"Zhentian", message:"Hello world!"       };       Observe.onnext (person);    Observe.oncompleted (); }Catch(Error) {Observe.onerror (error); }  }, 1000); //Note that this is optional, and you does not have the to return this if you require no cleanup  return function() {cleartimeout (ID); }});varSub = Source.subscribe (functionOnNext (person) {Console.log (Person.name+ ' say ' +person.message);},functionOnError (Err) {Console.log ("Error:" +err);},functiononcompleted () {Console.log ("Done");});/*"Error:there is an Error"*/

[RXJS] Creating an Observable with 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.