[英]execute a function after previous function is complete
How to call a function after the previous function is complete?上一个function完成后如何调用一个function? In the code below the
second_function()
is executed before the first_function()
and the data get suffled在下面的代码中,
second_function()
first_function()
执行并且数据被打乱
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);
}
The behaviour is expected, please have a look of the below example.该行为是预期的,请查看以下示例。
first_function(); second_function(); function name() { console.log("name"); } async function first_function() { await name(); console.log("first"); } function second_function() { console.log("second"); }
It can be written as它可以写成
function name() { console.log("name"); } function second_function() { console.log("second"); } Promise.resolve(name()).then(() => { console.log("first"); }); second_function();
Either do it like this要么这样做
first_function().then(() => first_function())
Or if you want to use the async\await syntax或者如果你想使用 async\await 语法
async function callingFunc() {
await first_function();
second_function();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.