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,050 points
27 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
• 27,630 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 84 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,010 points
44 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,010 points
760 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
123 views
0 votes
1 answer
+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,620 points
127 views
0 votes
1 answer
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
• 27,630 points
26 views