我有一個呼叫其他函式的輔助函式。我希望被呼叫的函式在繼續使用輔助函式之前完成。
目前他們都在同時改變事物。
helperDikjstras = async() => {
//do stuff
await this.colorVisited(visitedNodes); // I want this function to finish before continuing
await this.colorPath(path);
//EDIT: I've also tried:
this.colorVisited(visitedNodes).then(() => this.colorPath(path));
return;
}
colorVisited = async (visitedNodes) => {
//do stuff
return;
}
colorPath = async (path) => {
//do stuff
return;
}
uj5u.com熱心網友回復:
他們當然這樣做,這就是它的作業方式。JavaScript 中沒有“中斷”或暫停功能。為了實作您的目標,請在第一個 await 的后續 .then() 中呼叫您的后續函式。
像這樣:
helperDikjstras = async() => {/*...*/}
colorVisited = async(visitedNodes, _callback) => {/*...*/}
colorPath = async(path) => {/*...*/}
helperDikjstras()
.then(() => colorVisited(blah, halb))
.then(() => colorPath(somePath));
如果您需要在之后運行其他功能,colorPath()只需添加更多...
helperDikjstras()
.then(() => colorVisited(blah, halb))
.then(() => colorPath(somePath))
.then(() => doSomethingElse())
.then(() => andAnother())
.then(() => etc());
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/352436.html
標籤:javascript 异步 异步等待
