As the title suggests, I want to build a function that calls the other functions one after the other. Unfortunately, the functions take different lengths of time and understandably problems arise when they are executed.
Is there a simple solution to this problem?
function allInOne() {
loadData();
moveSheet();
sortStockData();
}
Before the first function is finished, the second has already been carried out. But it should all be done one after the other. I just got stuck through google etc.
Thank you in advance.
Normal Javascript is single-threaded, so when you call one function and then another and another (as you did) you can expect them to run in sequence.
However you will probably find that loadData() is an asynchronous function, returning a Promise
. Look at its return
statement to check this.
If it is returning a promise, you can say,
loadData().then(result => moveSheet())
If moveSheet
too, returns a promise, you can expand the chain as follows:
loadData().then(result => moveSheet()).then(result => sortStockData())
async function x(){
await loadData();
await moveSheet();
sortStockData();
}
More information on Promises is here.
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.