节点JS根据事件到达继续执行

时间:2015-01-15 22:20:46

标签: javascript node.js

我有一个Node JS应用程序,它监听从实体A的套接字接收的事件,并根据这些事件执行操作。此操作包括基于promises链接调用不同实体(包括A)。

我实际需要实现的是一种在实体A中调用操作并基于新事件而不是基于A响应执行回调的机制。所以流程将是:

return new RSVP.Promise(function (resolve, reject) {
    run action1();
    .then(function (response) {
        return action2();
    })
    .then(function (response) {
        return action3();
    })
    .then(function (response) {
        // code to wait for an event to arrive and resume this
    })
}

这可以实现吗?

1 个答案:

答案 0 :(得分:0)

你似乎并不需要周围的new Promise。假设run_action返回一个承诺,那么只需:

return run_action1()
    .then(function (response) {
        return action2();
    })
    .then(function (response) {
        return action3();
    })
    .then(function (response) {
        return new Promise(function(resolve, reject) {
            listen(resolve);
        });
    })
   .then(somethingElse);