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