[Ethereum] WEB3 accessing multiple return values

web3js

I have a contract deployed on the blockchain that returns multiple values.

contract Test{
   function getAandB( uint x )returns ( uint a, uint b ){

       uint y = x * 2 ;
       return ( x , y )
   }
   }

The question is how do I access both returning values when calling the contract externally from web3?

    var  ( a , b ) = myContractInstance.getAandB( x );      

gives me an error

   var   value  = myContractInstance.getAandB( x );
   console.log ( value.a )

comes back as undefined

Best Answer

With new Destructuring assignment syntax you can do this in one line:

let [a, b] = myContractInstance.getAandB.call(2);
console.log("a=", a);
console.log("b=", b);

Output is as expected:

a=2
b=4
Related Topic