I am new to JS and trying execute function2 after function1 is finished, below are my function that I am using. But with this implementation I see function2 gets executed before function1 is completely finished.
Function1
function repeatPatternColor(pattern) {
return new Promise((resolve) => {
for (let i = 0; i < pattern.length; i++) {
setTimeout(function () {
const color = pattern[i];
const singleCol = document.getElementById(color);
setTimeout(function () {
singleCol.classList.add(`${color}-light`);
}, (i + 1) * delayMileSec);
setTimeout(function () {
singleCol.classList.remove(`${color}-light`);
}, (i + 2) * delayMileSec);
console.log(color);
}, (i + 1) * delayMileSec);
}
resolve();
});
}
Function2
function generateColor() {
return new Promise((resolve) => {
disableDivs();
const randomNum = Math.floor(Math.random() * 4);
const id = color[randomNum].id;
pattern.push(id);
const singleCol = document.getElementById(id);
singleCol.classList.add(`${id}-light`);
setTimeout(function () {
singleCol.classList.remove(`${id}-light`);
enableDivs();
}, 1000);
console.log("Pattern: " + pattern);
resolve();
});
}
Calling Function
async function fnAsync() {
await repeatPatternColor(pattern);
await generateColor();
}
let response = fnAsync();
2
Answers
Instead of the model you have adopted currently, I would suggest you have a wait function like this:
then rewrite your "Function 1" using an asynchronous format, an example below:
As you can probably notice, the function is a lot simpler and now truly async! You can try this with your other function too, and as long as you use
await wait()
and use await for both functions on your fnAsync, it will work the way you want.The issue is that you are resolving the promise as soon as the loop completes. However,
setTimeout
does not stop execution of a loop, therefore theresolve
will be called before the first timeout even fires.Try the following.
The
promise
variable will hold the last "classList.remove" promise, which you canawait
after the loop, or simply return it – as shown in the codethe
fn
async function simplifies the code a little, takes a callback and waits fordelayMileSec
before resolving. You await when you add the "light" class, but don’t await when you remove it. You only await the last "remove" so your next function will be called at the appropriate time