简体   繁体   English

Node js中的多重承诺

[英]Multiple promises in Node js

I am new to nodejs and struggling to find the issue with the below piece of code. 我是NodeJS的新手,正在努力寻找下面的代码来解决问题。

All i am trying to achieve is, calling few methods one after other and if all the calls are successful, then return success else throw an error if any of the method fails. 我想要实现的是,一个接一个地调用几个方法,如果所有调用都成功,则返回成功,如果任何方法失败,则抛出错误。

Issue i have is, before all the method gets executed, the main method terminates even though the promises are yet to be fullfilled. 我的问题是,即使尚未兑现承诺,在执行所有方法之前,主方法也会终止。

Main method 主要方法

processRequest(neworder).then(function (done) {
    console.log('Successfully processed the order' + done);
    res.sendStatus(200);
}).fail(function (error) {
    res.status(404).send(error);
})

Other method calls 其他方法调用

module.exports.processRequest = function (Order) {
    var deferred = Q.defer();

    findX(Order)
        .then(findYBasedOnPrevOutput)
        .then(findZBasedOnPrevOutput)
        .then(deferred.resolve())
        .fail(function (err) {
            console.log('Failed to process request' + err);
            deferred.reject(err);
        });

    return deferred.promise;
}


var findX = function (order) {
    var deferred = Q.defer()

    db.list(order, function (address) {
        console.log('Query success');
        if (address == null)
            deferred.reject('Error');
        else {
            deferred.resolve(address);
        }
    })

    return deferred.promise;
};

Issue that i am having is, i see in the console the success just after the findX method gets called. 我遇到的问题是,在调用findX方法之后,我在控制台中看到了成功。 I expected the success msg after findZ method. 我期待findZ方法后成功味精。

Can you please help me in finding the issue with the above code, appreciate your input/suggestions in this regard 您能否帮助我找到上述代码的问题,感谢您在此方面的意见/建议

For simplicity, i didn't share the other modules here, but they are very similar to findX 为了简单起见,我在这里没有共享其他模块,但是它们与findX非常相似

I'd suggest you just use the promise you already have rather than creating a new one as both more efficient and to avoid a promise anti-pattern: 我建议您只使用已有的诺言,而不要创建新的诺言既更有效又避免了诺言反模式:

module.exports.processRequest = function (Order) {
  return findX(Order)
    .then(findYBasedOnPrevOutput)
    .then(findZBasedOnPrevOutput)
    .fail(function (err) {
      // log the error, then throw it again so it is returned as a rejected promise
      console.log('Failed to process request' + err);
      throw(err);
    });
}

And, I'd suggest changing findX to use Q's ability to turn a standard async call into one that returns a promise like this: 而且,我建议更改findX以使用Q的功能,以将标准异步调用转换为返回如下承诺的调用:

var findX = function(order) {
  return Q.nfcall(db.list.bind(db), order);
};

Or, combine the two like this: 或者,像这样结合两者:

module.exports.processRequest = function (Order) {
  return Q.nfcall(db.list.bind(db), Order)
    .then(findYBasedOnPrevOutput)
    .then(findZBasedOnPrevOutput)
    .fail(function (err) {
      // log the error, the throw it again so it is returned as a rejected promise
      console.log('Failed to process request' + err);
      throw(err);
    });
}

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

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