How to call a function after the previous function is complete? In the code below the second_function()
is executed before the first_function()
and the data get suffled
first_function();
second_function();
function name() {
if (condition == true) {
let tdbody = document.createElement('td');
tdbody.classList.add("class1");
let tdbodytext = document.createTextNode(userincourseres[i].fullname);
tdbody.appendChild(tdbodytext);
trbody.appendChild(tdbody);
}
}
async function first_function() {
await name();
if (condition == true) {
var tdbody = document.createElement('td');
var tdbodytext = document.createTextNode(get_grade_res.grade);
tdbody.classList.add("class2");
tdbody.appendChild(tdbodytext);
trbody.appendChild(tdbody);
}
}
function second_function() {
if (condition == true) {
var tdbody = document.createElement('td');
var tdbodytext = document.createTextNode(get_grade_res.grade);
tdbody.classList.add("class3");
tdbody.appendChild(tdbodytext);
trbody.appendChild(tdbody);
}
2
Answers
The behaviour is expected, please have a look of the below example.
It can be written as
Either do it like this
Or if you want to use the asyncawait syntax