[rxjs] Creating An Observable with RxJS
with an Rxjs creating Observable
2023-09-14 08:59:20 时间
Create an observable
var Observable = Rx.Observable; var source = Observable.create(function(observe){ var person = { name: "Zhentian", message: "Hello World!" }; observe.onNext(person); observe.onCompleted(); }); var sub = source.subscribe(function onNext(person){ console.log(person.name + ' say ' + person.message); }, function onError(err){ console.log(err); }, function onCompleted(){ console.log("done"); }); //Zhentian say Hello World! //done
Async
var Observable = Rx.Observable; var source = Observable.create(function(observe){ setTimeout(function(){ var person = { name: "Zhentian", message: "Hello World!" }; observe.onNext(person); observe.onCompleted(); }, 1000); console.log("ansyc finished!"); }); var sub = source.subscribe(function onNext(person){ console.log(person.name + ' say ' + person.message); }, function onError(err){ console.log(err); }, function onCompleted(){ console.log("done"); }); //"ansyc finished!" //"Zhentian say Hello World!" //"done"
Dispose the async
When you dispose the operation, we can see it log out "start timeout", which is not good, because, the onNext() would never be called, what we want is it even don't get inside setTimeout function.
var Observable = Rx.Observable; var source = Observable.create(function(observe){ setTimeout(function(){ console.log("Starat timeout"); var person = { name: "Zhentian", message: "Hello World!" }; observe.onNext(person); observe.onCompleted(); }, 1000); console.log("ansyc finished!"); }); var sub = source.subscribe(function onNext(person){ console.log(person.name + ' say ' + person.message); }, function onError(err){ console.log(err); }, function onCompleted(){ 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.
var Observable = Rx.Observable; var source = Observable.create(function(observe){ var id = setTimeout(function(){ console.log("Starat timeout"); var person = { name: "Zhentian", message: "Hello World!" }; observe.onNext(person); observe.onCompleted(); }, 1000); console.log("ansyc finished!"); // Note that this is optional, you do not have to return this if you require no cleanup return function(){ clearTimeout(id); } }); var sub = source.subscribe(function onNext(person){ console.log(person.name + ' say ' + person.message); }, function onError(err){ console.log(err); }, function onCompleted(){ console.log("done"); }); setTimeout(function(){ sub.dispose(); }, 500); /* "ansyc finished!" */
Catch error
If we throw an error in the code, but we found it actually not catched by the onError handler.
var Observable = Rx.Observable; var source = Observable.create(function(observe){ var id = setTimeout(function(){ throw "there is an error"; //Throw an error here var person = { name: "Zhentian", message: "Hello World!" }; observe.onNext(person); observe.onCompleted(); }, 1000); // Note that this is optional, you do not have to return this if you require no cleanup return function(){ clearTimeout(id); } }); var sub = source.subscribe(function onNext(person){ console.log(person.name + ' say ' + person.message); }, function onError(err){ console.log("Error: " + err); }, function onCompleted(){ console.log("done"); }); /* "error" "Uncaught there is an error (line 6)" */
What we can do is to add try catch in the block.
var Observable = Rx.Observable; var source = Observable.create(function(observe){ var id = setTimeout(function(){ try{ throw "there is an error"; //Throw an error here var person = { name: "Zhentian", message: "Hello World!" }; observe.onNext(person); observe.onCompleted(); }catch(error){ observe.onError(error); } }, 1000); // Note that this is optional, you do not have to return this if you require no cleanup return function(){ clearTimeout(id); } }); var sub = source.subscribe(function onNext(person){ console.log(person.name + ' say ' + person.message); }, function onError(err){ console.log("Error: " + err); }, function onCompleted(){ console.log("done"); }); /* "Error: there is an error" */
相关文章
- an软件怎么下载到电脑上,Adobe Animate(An)2023软件安装包下载及安装教程
- UnorderedObjectListWarning:Pagination May Yield Inconsistent Results With an Unordered Object_list
- AN动画制作软件需要的软件 An下载:Adobe Animate 2023中文汉化激活版
- ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc. ORACLE 报错 故障修复 远程处理
- ORA-02022: remote statement has unoptimized view with remote object ORACLE 报错 故障修复 远程处理
- ORA-19294: XQST0059: It is a static error if an implementation is unable to process a schema or module import by finding a schema or module with the specified target namespace ORACLE 报错 故障修复 远程处理
- ORA-24127: TABLESPACE parameter specified with an ACTION other than CREATE_ACTION ORACLE 报错 故障修复 远程处理
- ORA-26787: The row with key string does not exist in table string ORACLE 报错 故障修复 远程处理
- ORA-26919: Capture “string” is not associated with queue “string”.”string”. ORACLE 报错 故障修复 远程处理
- ORA-29515: exit called from Java code with status string ORACLE 报错 故障修复 远程处理
- ORA-39172: Cannot remap transportable tablespace names with compatibility of string. ORACLE 报错 故障修复 远程处理
- ORA-39322: Cannot use transportable tablespace with TIMESTAMP WITH TIME ZONE columns and different time zone version. ORACLE 报错 故障修复 远程处理
- ORA-39803: Data saves are not allowed on tables with partitioning referential constraints. ORACLE 报错 故障修复 远程处理
- ORA-51007: parameter [string] not registered with this check ORACLE 报错 故障修复 远程处理
- ORA-07222: slspool: line printer spooler command exited with an error. ORACLE 报错 故障修复 远程处理
- ORA-12522: TNS:listener could not find available instance with given INSTANCE_ROLE ORACLE 报错 故障修复 远程处理
- ORA-14114: partitioned table cannot have column with object, REF, nested table, array datatype ORACLE 报错 故障修复 远程处理
- ORA-15471: volume redundancy incompatible with diskgroup redundancy ORACLE 报错 故障修复 远程处理
- ORA-16407: An archive log from an earlier redo branch has been rejected ORACLE 报错 故障修复 远程处理
- ORA-16408: An archive log from an incompatible redo branch has been rejected. ORACLE 报错 故障修复 远程处理
- ORA-18003: an outline already exists with this signature ORACLE 报错 故障修复 远程处理
- 语句掌握Oracle中用WITH语句的利用技巧(oracle的with)
- Empowering Your eLearning with Linux Moodle: A Comprehensive Guide(linuxmoodle)
- MySQL查询优化使用WITH子句的限制与替代方案(mysql不能用with)