Naz*_*iuk 6 javascript observable rxjs typescript ethereum
我需要从中观察
window.web3.eth.getCoinbase((error, result) => { ... });
Run Code Online (Sandbox Code Playgroud)
这是个好主意吗?
new Observable<string>(o => {
this.w.eth.getCoinbase((err, result) => {
o.next(result);
o.complete();
});
});
Run Code Online (Sandbox Code Playgroud)
RxJS包括一个bindNodeCallback可观察的创建器,专门用于从使用Node样式回调的异步函数创建可观察的对象。
您可以这样使用它:
const getCoinbaseAsObservable = Observable.bindNodeCallback(
callback => this.w.eth.getCoinbase(callback)
);
let coinbaseObservable = getCoinbaseAsObservable();
coinbaseObservable.subscribe(
result => { /* do something with the result */ },
error => { /* do something with the error */ }
);
Run Code Online (Sandbox Code Playgroud)
请注意,箭头函数用于确保使用getCoinbase方法this.w.eth作为其上下文来调用该方法。
| 归档时间: |
|
| 查看次数: |
1755 次 |
| 最近记录: |