Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Problem with promisses #4

Closed
mhhf opened this issue Jul 29, 2015 · 4 comments
Closed

Problem with promisses #4

mhhf opened this issue Jul 29, 2015 · 4 comments

Comments

@mhhf
Copy link

mhhf commented Jul 29, 2015

I have difficulties to work with Promises:

Every snipped is run in the browser truffle(v0.0.13) env with a fresh chain(testrpc) and contract deployment.
I use the Coin Contract from the Solidity Tutorial.

The following web3 code works well:

a = web3.eth.accounts[0];
C = web3.eth.contract(Coin.abi);
c = C.at( Coin.deployed_address );
c.mint(a,42);
c.queryBalance( a ); // BigNumber(42)  

but the following pudding code never return its promise:

a = web3.eth.accounts[0]
c = Coin.at( Coin.deployed_address );
c.mint( a, 42 ).then( function() {
  c.queryBalance(a)
  .then( function( v ){ 
    console.log( v ); // never gets called
  });
})

The code works well until the correct balance(42) is received via rpc.
Then suddenly every second the following rpc request is send:

{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["42"],"id":5}

with following response

{"error": {"message": "Invalid parameters.", "code": -32602}, "jsonrpc": "2.0", "id": 5}

where to go from here?

@tcoulter
Copy link
Contributor

Hi @mhhf. Try this for me:

a = web3.eth.accounts[0]
c = Coin.at( Coin.deployed_address );
c.mint( a, 42 ).then( function() {
  return c.queryBalance(a);
}).then( function( v ){ 
  console.log( v ); // should get called
});

If you return a promise within a promise's handler, it will continue executing that promise chain. So in this case, v will be the return value from c.queryBalance(a); (v should be a transaction hash since it looks like queryBalance is a transaction). Make sense?

@tcoulter
Copy link
Contributor

Edited the comment above; lots of typos.

@mhhf
Copy link
Author

mhhf commented Jul 29, 2015

Works like a charm!

I should have read about promises, d'oh!

@mhhf mhhf closed this as completed Jul 29, 2015
@tcoulter
Copy link
Contributor

Cheers! Let me know if you have anymore questions!

mhhf added a commit to mhhf/ether-pudding that referenced this issue Aug 25, 2015
Constant functions don't change the state and arn't executing a
transaction, therefore they don't need to be synchronized.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants