Now I'm trying to delelop my first Dapp (using Metamask plugin). everything is going to be all right, but I can't get one thing: Is it possible to receive return from function, using Web3 (not JS VM!)

For example, I have a simple contract like this:

pragma solidity ^0.4.0;

contract test {
    int32 data = 123;

    function getData() returns (int32) {
        return data;

When I'm calling getData() with JS virtual machine, it works fine:


But with selected Inject web3 (Metamask) it returns information about new transaction, instead of 123


I also can't receive any data from the JS console (Metamask):

enter image description here

  • 1
    The last example in the JS console looks like it should work. Maybe this question needs to focus on that part. – eth Apr 5 '17 at 23:43
  • @eth Yep, it works fine, but I get txn hash instead of 123, as expected – Sergey Potekhin Apr 5 '17 at 23:47
  • 1
    Sorry, I misread the console output as error being undefined and data as null. The txn hash is returned because a transaction was issued by web3 instead of a "call" (hope you saw the question that asks about the difference). – eth Apr 6 '17 at 4:28

For the question, the simplest is to make getData constant as:

function getData() constant returns (int32)

Another option is to use an event: How to get return values when function with argument is called?

  • Adding constant modifier solved all, thanks a lot! – Sergey Potekhin Apr 6 '17 at 1:08

Did you try using the call function asynchronously:

contract.getData.call().then(function(result) {
  • I've had pretty good success using Promise.promisifyAll (after contract instantiation) so you can use actual return values (Promises) instead of callbacks, if return values are important to you. – Paul S Apr 6 '17 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.