2

I'm a newcomer to Ethereum, working on a project that requires a certain piece of data from a contract, in order to populate a list in my HTML frontend. However, as you might expect, the calls to the Contracts, not being instantaneous, aren't finishing before the rest of the code is executed, resulting in the relevant values being filled in with UNDEFINED. From the Console, I know that these values come through eventually - it's just that they're out of order. I'm well aware that something like this requires Promises, or Callbacks, or some variant of such, but I am simply having no luck getting any such thing to work, and I'm not sure how they work with Ethereum. I've scoured for tutorials, guides, etc, but I've still not had any joy. I've got a few calls but since they're all pretty much the same (just calling different methods and getting slightly different response variables), I'll just post one:

try{
RegionalContract.areYouInList(codeInput.value, function(error, res){
if(!error) {
var areWeAllowed = res
console.log("Are we allowed to vote? : " + areWeAllowed)
}
});
}catch  (err) {
console.log(err)
}

How would I go about setting up a Promise, et al, to make the page wait until this method had returned? Thanks a bunch.

closed as off-topic by Ismael, Achala Dissanayake, Richard Horrocks, mirg, norganna Apr 23 '18 at 13:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – Ismael, Achala Dissanayake, Richard Horrocks, mirg, norganna
If this question can be reworded to fit the rules in the help center, please edit the question.

  • This is not about Ethereum, it is about how async functions works in javascript. This will happen with any library that requires async functions. – Ismael Apr 21 '18 at 22:02
  • 1
    What I dislike about these platforms is that when someone asks something who has a high rep then nothing happens, but when someone asks a similar question who has a low rep it is closed. This question shows how an async question was asked by someone with high rep and no one closed it - ethereum.stackexchange.com/questions/11444/… This basic theme I have seen numerous times. This is a form of bullying by people who have high reps against others. – – Trevor Oakley Jun 11 '18 at 10:18
1

This can be achieved with "Async - await" syntax.

Website below explains really nicely how it works. https://javascript.info/async-await

If you want some examples of using async/await in Solidity, you can also read this article: https://medium.com/@angellopozo/testing-solidity-with-truffle-and-async-await-396e81c54f93

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