I need to make observable from
window.web3.eth.getCoinbase((error, result) => { ... });
Is it a good idea?
new Observable<string>(o => {
this.w.eth.getCoinbase((err, result) => {
o.next(result);
o.complete();
});
});
I need to make observable from
window.web3.eth.getCoinbase((error, result) => { ... });
Is it a good idea?
new Observable<string>(o => {
this.w.eth.getCoinbase((err, result) => {
o.next(result);
o.complete();
});
});
RxJS includes a bindNodeCallback
observable creator specifically for creating observables from async functions that use Node-style callbacks.
You could use it like this:
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 */ }
);
Note that an arrow function is used to ensure the getCoinbase
method is called using this.w.eth
as its context.
© 2022 - 2024 — McMap. All rights reserved.
fromEvent()
– StivergetCoinbase
. The callback is only fired once (The API switched to Promise in web3 1.0.0). – MinnesingerbindNodeCallback
. – Leann