I am retrieving data from mysql and looping through each row. up till here everything executes in the order I want it to. However in the same loop I need to call a smart contract function in order to verifyHash and return a value of true or false and then if true store data in an array of objects. However the call im using is asynchronous so it doesn't let it execute first before moving on, so its a complete confusion.


I would like to call the same method synchronously inside the for loop.

Thanks in advance.


Check out async/await https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function][1] It makes code lines yield to the ones before them.

Basically, you are going to define the function that is being called for each row as a async function, like async function rowHandler()

Within that function do the contract call as follows let result = await contract.methods.verifyHash(id, hash, addr)

You can then check the result, and do the subsequent operations you need to.

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.