简体   繁体   English

等待承诺-代码挂起

[英]Waiting for promises - code hangs

I am using Javascript Promises for the first time and ran into something I don't understand. 我第一次使用Javascript Promises,遇到了我不了解的东西。

What I am trying to do is create a validation phase which runs around and checks things - eventually waiting for all promises to resolve. 我要尝试做的是创建一个验证阶段,该阶段将运行并检查事物-最终等待所有诺言解决。

To do this, I create a validation promise: 为此,我创建了一个验证承诺:

 validate = function(data) {
     var p = new Promise(function(resolve, reject)){

Here I define a promises array for all the different things I will be doing: 在这里,我将为将要执行的所有不同操作定义一个promises数组:

         var all_promises = Array();

Now do things like this Sequelize call while adding promises into this array (Sequelize returns promises): 现在,在将promise添加到该数组中的同时执行类似Sequelize调用的操作(Sequelize返回promise):

         all_promises.push(resBooking);
         resBooking.count(...).then(...).catch(...);

I have logging statements that demonstrate we got through then and everything is dandy. 我有记录日志,表明我们到那时为止,一切都很好。 Now all I need to do is wait! 现在我所要做的就是等待!

        Promise.all(all_promises).then(function(){
            p.resolve();
        });

But this silly thing hangs - it waits for something to complete. 但是,这个愚蠢的事情挂了-等待完成。 No CPU usage. 无CPU使用率。 What am I doing wrong? 我究竟做错了什么?

What you want is 你想要的是

validate = function(data) {
    var p = new Promise(function(resolve, reject)){
        var all_promises = Array();
        all_promises.push(resBooking);
        resBooking.count(...).then(...).catch(...);

        Promise.all(all_promises).then(resolve);
    });
    return p;
};

In other words, call resolve , not p.resolve() . 换句话说,调用resolve ,而不是p.resolve() p.resolve will generate a run-time error ( p doesn't exist), which will be "swallowed" by the promise constructor and cause it to fail. p.resolve将生成一个运行时错误( p不存在),该错误会被promise构造函数“吞噬”并导致失败。 However, you won't even be able to see that rejected promise from the outside, since you are also not returning it from the function. 但是,您甚至无法从外部看到被拒绝的承诺,因为您也不会从函数中返回它。

However, although this code should work now, you are still committing the "promise constructor anti-pattern". 但是,尽管此代码现在应该可以使用,但是您仍在提交“ promise构造函数反模式”。 You don't need to construct a new promise when you already have one in the form of the Promise.all . 当您已经有了Promise.all形式的承诺时,就不需要构造新的承诺。 So you can just write 所以你可以写

validate = function(data) {
    var all_promises = Array();
    all_promises.push(resBooking);
    resBooking.count(...).then(...).catch(...);
    return Promise.all(all_promises);
};

I am not sure the above is exactly what you want, though. 我不确定以上内容是否正是您想要的。 I don't know what resBooking is, or resBooking.count . 我不知道什么是resBookingresBooking.count In any case, you will be waiting on the resBooking promise, rather than the result of the then and catch you are hanging off it. 在任何情况下,你会被等候在resBooking承诺,而不是的结果thencatch你挂吧。 Depending on what you're trying to accomplish, you might want 根据您要完成的工作,您可能想要

validate = function(data) {
    var all_promises = Array();
    all_promises.push(resBooking.count(...).then(...).catch(...));
    return Promise.all(all_promises);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM