0

I have a function that calls a contract and uses the return value in the code. I want to change that function so I can use it in the rest of my code as getter to my contract.

The function is asynchronous, so I can't do something like var res = App.getter(param);.

Looking at the other asynchronous functions like the calls, I figured I could use .then or promises: ContractInstance.myFunction.call().then(function (result){ ... });

But App.myFunction(i).then(function (result){ ... }); doesn't work, and I don't know where to look to make it the way I want.


Here's the function:

getEventInfo: function(i){
  var numeroPlace;
  App.contracts.myContract.at(addresses[i]).then(function (instance) {
    var MyContractInstance = instance;
    MyContractInstance.getEventInfo.call().then(function (result) {
        console.log(result);
        //do stuff with result
        return result;
    });
  });
},

I want to be able to do something like:

var eventInfo = App.getEventInfo(i);
//do stuff with eventInfo

or

App.getEventInfo(i).then(function (result){
  var eventInfo = result
  //do stuff with eventInfo
};
1

Depending on how you have truffle setup, you can use [CONTRACT].deployed() which is a promise that returns the instance, and you can opt to use then/catch or async/await (my preferred method if you are using ES6).

// async / await

contract.deployed().then(async (instance) => {
  const get = await instance.myFunction({ from: account })
  // ...additional code here.
})


// .then/.catch

contract.deployed().then((instance) => {
  instance.myFunction({ from: account }).then((data) => {
    // ...additional code here.
  })
})

Response to EDIT:

Ahh, gotcha. You simply need to nest your returns.

getEventInfo: function(i){
  // var numeroPlace;
  return App.contracts.myContract.at(addresses[i]).then(function (instance) {
    return instance.getEventInfo.call()
  }).then(function(result) {
    console.log(result);
    //do stuff with result
    return result;
  });
}

...but since you are now essentially returning a promise, var eventInfo = App.getEventInfo(i); will have to change to wait for that promise to resolve.

var eventInfo = await App.getEventInfo(i); will do that for you, but, wherever this is called must be inside of an async parent function. So if you set this eventInfo variable inside of a function called loadEvent, then:

async function loadEvent() {
  // ...additional stuff here.
  var eventInfo = await App.getEventInfo(i);
  // ...additional stuff here.
}

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

  • I edited the question to add my functions, it's much more clearer. The function already works, but I'd like to use it as a function that returns something, not do the work inside the function. – Teleporting Goat Mar 27 '18 at 15:25
  • @TeleportingGoat gotcha. Answer updated. – ReyHaynes Mar 27 '18 at 15:38
  • Thanks. I made async work with the example in the docs but it doesn't work with my function. First eventInfo is set to undefined, then after some time getEventInfo() returns its value. Do you have an idea why that's happening? – Teleporting Goat Mar 27 '18 at 16:12
  • Does App.getEventInfo() need to be async too? await clearly doesn't wait for it to finish. Or maybe have a promise in it? It's just a function call so maybe it doesn't appear as asynchronous. – Teleporting Goat Mar 27 '18 at 16:20
  • @TeleportingGoat Edited getEventInfo. It was not waiting for the return inside the getEventInfo.call(). Had to pass the getEventInfo.call() promise in a subsequent promise chain. – ReyHaynes Mar 27 '18 at 16:45

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.