I know that javascript is asynchronous and if i make a for loop after .then
then the loop ends and only then do the object promise become clear, but I can't for the life of me fix this code snippet, maybe someone can help me. My goal is to loop and check if variable ans
, which I take from a function is equal to variable account
and if so then print out information that I get from other functions.
loopforCetrs : function() {
var num;
var account = web3.currentProvider.selectedAddress;
App.contracts.StudentState.deployed().then(function (instance) {
return instance.showNumOfContracts();
}).then(function (numOfCert) {
num = numOfCert;
var wrapper = document.getElementById("myHTMLWrapper");
for (var i = 0; i < num; i++) {
App.ShowAddress(i).then(function (ans) {
if(ans == account) {
alert(ans+' Hello');
alert(account+' Hi')
App.ShowFName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Name: ' + ans + ' </span><br/><br/>';
})
App.ShowLName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Surname: ' + ans + ' </span><br/><br/>';
})
App.ShowInstName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Institutions name: ' + ans + ' </span><br/><br/>';
})
App.ShowAddress(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Users address: ' + ans + ' </span><br/><br/>';
})
App.ShowCourseName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Course name: ' + ans + ' </span><br/><br/>';
wrapper.innerHTML += '<span class="test"></span><br/><br/>';
})
}
})
}
})
},
EDIT 1: This was the code i used before and it did the job, but now I wanted to add 1 thing and hit a wall.
loopforCetrs : function() {
var num;
var account = web3.currentProvider.selectedAddress;
App.contracts.StudentState.deployed().then(function (instance) {
return instance.showNumOfContracts();
}).then(function (numOfCert) {
num = numOfCert;
var wrapper = document.getElementById("myHTMLWrapper");
for (var i = 0; i < num; i++) {
App.ShowFName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Name: ' + ans + ' </span><br/><br/>';
})
App.ShowLName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Surname: ' + ans + ' </span><br/><br/>';
})
App.ShowInstName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Institutions name: ' + ans + ' </span><br/><br/>';
})
App.ShowAddress(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Users address: ' + ans + ' </span><br/><br/>';
})
App.ShowCourseName(i).then(function (ans) {
wrapper.innerHTML += '<span class="test">Course name: ' + ans + ' </span><br/><br/>';
wrapper.innerHTML += '<span class="test"></span><br/><br/>';
})
}
})
},