I have a asyn function in which I call another function that return a promise and also calls to another async function. Here is the code:
JavaScript
x
13
13
1
async function asyncAwaitRepeat(index) {
2
if(index < 9) {
3
4
await promise_animate(rectPointer); // the promise_animate return a promise
5
6
await asyncAwaitRepeat(index + 1); // but my promise_animate never gets resolved
7
8
9
}
10
}
11
12
asyncAwaitRepeat(asyncAwaitRepeatindex);
13
since I have some asynchronous code to execute so I cannot invoke resolve() directly. so I use call back
JavaScript
1
8
1
function promise_animate (index) {
2
return new Promise((resolve) => {
3
4
animate(index, () => {resolve}); // since animate function is a asynchronous so we
5
//cannot directly resolve() it
6
})
7
}
8
and the animate function itself is async doing a bunch of asynchronous stuff
JavaScript
1
31
31
1
async function animate(rectIndex, animationPromiseFulfilled) {
2
if(rectIndex < 8) {
3
4
await promise_hightlight(rectIndex, "red");
5
6
if( rectArray[rectIndex].value > rectArray[rectIndex+1].value ) {
7
await promise_hightlight(rectIndex, "red");
8
9
// doing a bunch of asynchronous stuff
10
11
await animate(rectIndex+1, animationPromiseFulfilled); // << Here i call the
12
// function recursively
13
}
14
else if(rectIndex == 8) {
15
16
await promise_hightlight(rectIndex, "red");
17
18
if( rectArray[rectIndex].value > rectArray[rectIndex+1].value ) {
19
await promise_hightlight(rectIndex, "red");
20
21
// some more asynchronous stuff
22
23
}
24
await promise_hightlight(rectIndex, "green");
25
rectPointer = 0;
26
animationPromiseFulfilled("the animation function resolved");//<< the promise
27
//resolve done
28
}
29
30
}
31
But the fullfiled() is not doing its work and not resolving. Pls help
Advertisement
Answer
I think your resolve never getting called so you have to try like that:
JavaScript
1
6
1
function promise_animate (index) {
2
return new Promise(async(resolve) => {
3
resolve(await animate(index));
4
})
5
}
6