Promise永远不会在javascript中解决

尤塞夫

我有一个异步函数,在其中调用另一个函数返回一个promise,同时还调用另一个异步函数。这是代码:

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);

因为我有一些异步代码要执行,所以我不能直接调用resolve()。所以我用回电

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

animate函数本身是异步的,做了很多异步的事情

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 
    }
     
}

但是fullfiled()没有完成其工作,也没有解决。请帮助

RK_oo7

我认为您的决心永远不会得到呼唤,因此您必须尝试这样:

 function promise_animate (index) {
        return new Promise(async(resolve) => {
            resolve(await animate(index));  
        })
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

方法onPostExecute(String)永远不会在本地使用-AsyncTask

来自分类Dev

永远不会在相应的try语句的主体中引发异常

来自分类Dev

WebRTC永远不会在IceCandidate上触发

来自分类Dev

Watchify更新事件永远不会在Virtualbox中触发

来自分类Dev

execvp()永远不会在管道上完成

来自分类Dev

ExecutorService Runnable永远不会在发生异常时尝试

来自分类Dev

WKInterfaceTable的didSelectRowAtIndex永远不会在WKInterfaceController中调用

来自分类Dev

编译错误:永远不会在相应的try语句的主体中引发异常

来自分类Dev

Premake生成的解决方案不会在Release中编译,但会在Debug中编译

来自分类Dev

GraphRequest永远不会在完成时加注

来自分类Dev

拥有的类型集合永远不会在EF Core中更新

来自分类Dev

永远不会在相应的try语句的主体中引发异常XXX

来自分类Dev

为什么我的ibjects不会在javascript中变形?

来自分类Dev

isArray检查和推断类型永远不会在TypeScript中

来自分类Dev

Ajaxrequest不会在Javascript中调用函数

来自分类Dev

永远不会在when.apply()。done()中输入的延迟Ajax请求数组

来自分类Dev

断言语句永远不会在异步测试中执行

来自分类Dev

异步任务永远不会在简单的API客户端中结束。僵局?

来自分类Dev

WKInterfaceTable的didSelectRowAtIndex永远不会在WKInterfaceController中调用

来自分类Dev

框阴影不会在JavaScript中“消失”

来自分类Dev

C ++相同的代码永远不会在Visual Studio中编译/运行,有时甚至不会在Qt Creator中运行

来自分类Dev

表单永远不会在symfony2中提交

来自分类Dev

Ajax done()和always()永远不会在Firefox中触发

来自分类Dev

永远不会在类classname中调用public classname()-Java

来自分类Dev

调试错误:永远不会在相应的try语句的主体中引发异常

来自分类Dev

ASPX Webform - 以编程方式创建的链接按钮永远不会在 UpdatePanel 中触发事件

来自分类Dev

Turbolinks 加载事件永远不会在全新的 Rails 5.1 应用程序中触发

来自分类Dev

Firestore fetch 永远不会在 Flutter 代码中执行

来自分类Dev

新窗口加载永远不会在 javascript 中触发

Related 相关文章

  1. 1

    方法onPostExecute(String)永远不会在本地使用-AsyncTask

  2. 2

    永远不会在相应的try语句的主体中引发异常

  3. 3

    WebRTC永远不会在IceCandidate上触发

  4. 4

    Watchify更新事件永远不会在Virtualbox中触发

  5. 5

    execvp()永远不会在管道上完成

  6. 6

    ExecutorService Runnable永远不会在发生异常时尝试

  7. 7

    WKInterfaceTable的didSelectRowAtIndex永远不会在WKInterfaceController中调用

  8. 8

    编译错误:永远不会在相应的try语句的主体中引发异常

  9. 9

    Premake生成的解决方案不会在Release中编译,但会在Debug中编译

  10. 10

    GraphRequest永远不会在完成时加注

  11. 11

    拥有的类型集合永远不会在EF Core中更新

  12. 12

    永远不会在相应的try语句的主体中引发异常XXX

  13. 13

    为什么我的ibjects不会在javascript中变形?

  14. 14

    isArray检查和推断类型永远不会在TypeScript中

  15. 15

    Ajaxrequest不会在Javascript中调用函数

  16. 16

    永远不会在when.apply()。done()中输入的延迟Ajax请求数组

  17. 17

    断言语句永远不会在异步测试中执行

  18. 18

    异步任务永远不会在简单的API客户端中结束。僵局?

  19. 19

    WKInterfaceTable的didSelectRowAtIndex永远不会在WKInterfaceController中调用

  20. 20

    框阴影不会在JavaScript中“消失”

  21. 21

    C ++相同的代码永远不会在Visual Studio中编译/运行,有时甚至不会在Qt Creator中运行

  22. 22

    表单永远不会在symfony2中提交

  23. 23

    Ajax done()和always()永远不会在Firefox中触发

  24. 24

    永远不会在类classname中调用public classname()-Java

  25. 25

    调试错误:永远不会在相应的try语句的主体中引发异常

  26. 26

    ASPX Webform - 以编程方式创建的链接按钮永远不会在 UpdatePanel 中触发事件

  27. 27

    Turbolinks 加载事件永远不会在全新的 Rails 5.1 应用程序中触发

  28. 28

    Firestore fetch 永远不会在 Flutter 代码中执行

  29. 29

    新窗口加载永远不会在 javascript 中触发

热门标签

归档