0

Following asynchronous function call returns value of total votes in variable v correctly

return GlobalcontractInstance.totalVotesFor.call(candidateName).then(function(v) {
$("#" + div_id).html(v.toString());
$("#msg").html("");
});

But following synchrounous function does not return value in no_of_votes.

no_of_votes = GlobalcontractInstance.totalVotesFor.call(candidateName);
$("#" + div_id).html(JSON.stringify(no_of_votes));

I am not able to figure out why it is happening so ? What is preferable way to call a smart contract functions?

1

It is not recommended to rely on synchronous functions. For example web3 v1.0 beta has completely removed support for them, and truffle never supported them in recent versions.

Modern browsers have support for async/await and you can use a transpiler to support old versions.

no_of_votes = await GlobalcontractInstance.totalVotesFor.call(candidateName);
$("#" + div_id).html(JSON.stringify(no_of_votes));
| improve this answer | |

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.