Skip to content
Advertisement

Promise never gets resolved in javascript

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:

async function asyncAwaitRepeat(index) {
    if(index < 9) {
      
            await promise_animate(rectPointer); // the promise_animate return a promise
            
            await asyncAwaitRepeat(index + 1); // but my promise_animate never gets resolved
        

    }
}

asyncAwaitRepeat(asyncAwaitRepeatindex);

since I have some asynchronous code to execute so I cannot invoke resolve() directly. so I use call back

function promise_animate (index) {
    return new Promise((resolve) => {
       
        animate(index, () => {resolve});  // since animate function is a asynchronous so we 
                                        //cannot directly resolve() it
    })
}

and the animate function itself is async doing a bunch of asynchronous stuff

async function animate(rectIndex, animationPromiseFulfilled) {
    if(rectIndex < 8) {
        
        await promise_hightlight(rectIndex, "red");
        
        if( rectArray[rectIndex].value > rectArray[rectIndex+1].value ) {
            await promise_hightlight(rectIndex, "red");
            
          // doing a bunch of asynchronous stuff

           await animate(rectIndex+1, animationPromiseFulfilled); // << Here i call the 
                                                               // function recursively
    }
    else if(rectIndex == 8) {
   
        await promise_hightlight(rectIndex, "red");
        
        if( rectArray[rectIndex].value > rectArray[rectIndex+1].value ) {
            await promise_hightlight(rectIndex, "red");
          
            // some more asynchronous stuff 
            
        }   
        await promise_hightlight(rectIndex, "green");               
        rectPointer = 0;
        animationPromiseFulfilled("the animation function resolved");//<< the promise 
                                                                     //resolve done 
    }
     
}

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:

 function promise_animate (index) {
        return new Promise(async(resolve) => {
            resolve(await animate(index));  
        })
  }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement