Hey everybody ☺
I have a problem with calling functions in javascript for my dApp and hope someone of you can help me out...
I have those two functions:
function a (){
var myArray = [];
// for loop which fills up the array
return myArray;
}
function b (){
var dummy = a();
var lengthOfDummy = dummy.length;
console.log(dummy);
console.log(lengthOfDummy);
}
My console now prints out this:
//Dummy
[]
// lenght of dummy
0
A few seconds later, it updates to:
// Dummy
["0xkadj", "0xhuad", "0xhhadh"];
// length of dummy
0
Can someone tell me how I can assure that the variable will be set AFTER the function a has been executed for my "dummy" variable?
I googled for solutions, finding stuff like Promises and timeouts, but I was simply to dump to adapt the solutions I found. Would be great if someone can clear this up for me.
Thank you guys in advance!
Jun 3, 2018, 8:45 PM
How do u fill myArray? If there is any async call in the function you should be use callback, or promise to ensure function b called right after function a finished
Javascript is an asyncronous language. Not like python, or other
Jun 4, 2018, 1:55 AM
It is a for loop that fills the array. Going over a mapping of my smart contract, filtering out unique values an pushing them to myArray
Jun 4, 2018, 5:04 AM
reading smartcontract from js always asyncronous call, if its called within function A, its mean function A will be asycronous too
there is a simple solution if you have latest dev environment. add "await" before async function inside the loop. google "async await js"
Jun 4, 2018, 5:12 AM
Thanks for your help, will do that once I’m back home from work! 👍
Jun 4, 2018, 5:13 AM