How to make Observable from callback

0 votes

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();
    });
});
Oct 1, 2018 in Blockchain by slayer
• 29,350 points
2,459 views

1 answer to this question.

0 votes

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.ethas its context.

answered Oct 1, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

How to make sure transactions take no fee in a private Ethereum blockchain?

In a private ethereum network you have ...READ MORE

answered Mar 26, 2018 in Blockchain by Christine
• 15,790 points

edited Mar 26, 2018 by Christine 1,364 views
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,100 points
563 views
+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 17,100 points
2,603 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,692 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,233 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
961 views
0 votes
1 answer

How to achieve consensus with peers from different departments?

Defining one MSP to represent each division. ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 26,740 points
465 views
0 votes
1 answer

How to control bitcoind from another machine?

In your bitcoin.config file replace rpcallowip with ...READ MORE

answered Aug 17, 2018 in Blockchain by digger
• 26,740 points
673 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP