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();
});
});
Creating Observableslink The following example creates an Observable to emit the string 'hi' every second to a subscriber. import { Observable } from 'rxjs'; const observable = new Observable(function subscribe(subscriber) { const id = setInterval(() => { subscriber. next('hi') }, 1000); });
An observable produces values over time. An array is created as a static set of values. In a sense, observables are asynchronous where arrays are synchronous.
In Angular we can subscribe to an observable in two ways: Manner 1: We subscribe to an observable in our template using the async pipe. The benefit of this is that Angular deals with your subscription during the lifecycle of a component.
SubscribinglinkAn Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With