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 Answer 1

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));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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