我有三个功能,需要一个接一个地执行
CLUSTERED INDEX : CompletedQuestionnaireId;
CREATE NONCLUSTERED INDEX NIX_Dairy_data_Summary
ON [Report_performTest] SiteID,SubjectNo
INCLUDE All selected columns
CREATE NONCLUSTERED INDEX NIXQues_Dairy_data_Summary
ON [Report_performTest](Questionnaire_Number)
INCLUDE All selected columns
我想在$(document).ready(function(){
// Async function
getAllTask();
// Need to call after above function completed its process
loadTasks();
initView();
listenEvents();
});
async function getAllTask(){
// For this ap.request alone I used await, Because of await keyword I make this function as async
await AP.request(...);
}
函数完成其处理后执行所有其他函数。
但是函数getAllTask()
是异步的。请帮我解决这个问题。
有人遇到同样的问题吗?
答案 0 :(得分:1)
在调用getAllTask()时也使用await;并使函数异步,这样代码将等待函数完成
$(document).ready(async function(){
// Async function
await getAllTask();
// Need to call after above function completed its process
loadTasks();
initView();
listenEvents();
});
async function getAllTask(){
// For this ap.request alone I used await, Because of await keyword I make this function as async
await AP.request(...);
}
您还可以实现Promise对象=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 用resolve()完成异步功能,这里是一个示例
$(document).ready(async function(){
// Async function
await getAllTask().then((data)=>{
//Here you will get resolve(data)
console.log(data)
}).catch((err)=>{
console.log(err)
//here you will get error given
})
// Need to call after above function completed its process
loadTasks();
initView();
listenEvents();
});
async function getAllTask(){
return new Promise((resolve,reject)=>{
// For this ap.request alone I used await, Because of await keyword I make this function as async
await AP.request(...).then((data)=>{
//It will resolve data, same as return
resolve(data)
}
.catch((err)=>{
//It will return the error given
reject(err)
};
}
}