在loopbackJS中返回带有节点异步瀑布的结果

时间:2017-12-26 11:03:34

标签: node.js asynchronous loopbackjs node-async

据我所知,使用最新版本的async waterfall,cb已不再可用。

既然如此,如何为远程方法提供响应?我似乎无法在任何地方找到这个解释。

使用异步文档中的示例。

async.waterfall([
    function(callback) {
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback) {
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback) {
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
    // result now equals 'done'

    // HOW DO I RETURN result TO THE CALLER OF THE REMOTE METHOD?
    // HOW DO I RETURN result TO THE CALLER OF THE REMOTE METHOD?
    // HOW DO I RETURN result TO THE CALLER OF THE REMOTE METHOD?


});

编辑:这是我尝试传回响应的实际事件。在以前版本的async中,这只是通过将其传递给cb()来完成的。但是看起来async不再支持它。

Ticket.addComment = function( id, comment, postedBy, cb ) {

    async.waterfall([

      //get ticket and add content
      function(callback){
        Ticket.findById( id, function( err, ticket ){
          ticket.ticketComments.create({ "body": comment });
          callback(null, ticket);
        });
      },

      //update ticket isWith
      function(ticket, callback){
        ticket.save(null, {
          "id": ticket.id,
          "isWith": postedBy
        });
        callback(null,ticket);
      }

    ], function( err, ticket ){

       // I NEED TO RETURN "ticket" TO THE METHOD CALLER.. THIS USED TO BE DONE BY PASSING "ticket" INTO cb().

    });

  }

  Ticket.remoteMethod('addComment', {
    http: { verb: 'post'},
    accepts: [
      {arg: 'id', type: 'string', description: 'ticket id of the ticket the comment is to be added to'},
      {arg: 'comment', type: 'string', description: 'the comment body'},
      {arg: 'postedBy', type: 'string', description: 'Who posted the comment'}
    ],
    returns: {arg: 'comment', root: true, type: 'application/json'}
  });

2 个答案:

答案 0 :(得分:0)

 var create = function (req, res) {
        async.waterfall([
            _function1(req),
            _function2,
            _function3
        ], function (error, success) {
            if (error) { alert('Something is wrong!'); }
            return alert('Done!');
        });
    };
    function _function1 (req) {
        return function (callback) {
            var something = req.body;
            callback (null, something);
       }
    }

    function _function2 (something, callback) {
        return function (callback) {
           var somethingelse = function () { // do something here };
           callback (err, somethingelse);
        }
    }

    function _function3 (something, callback) {
        return function (callback) {
          var somethingmore = function () { // do something here };
          callback (err, somethingmore);
        }
    }

答案 1 :(得分:0)

简短回答:你不。这不是异步编程的方式。

是否有可能设法让调用者等待异步调用结束并获得结果。但这样做会破坏异步编程的目的。

所以,如果你的代码需要async.waterfall的结果,那么必须放在最后的回调中。

以下是一个如何实现这一目标的例子:

Ticket.addComment = function( id, comment, postedBy, cb ) {

  async.waterfall([

    //get ticket and add content
    function(callback){
      Ticket.findById( id, function( err, ticket ){
        ticket.ticketComments.create({ "body": comment });
        callback(null, ticket);
      });
    },

    //update ticket isWith
    function(ticket, callback){
      ticket.save(null, {
        "id": ticket.id,
        "isWith": postedBy
      });
      callback(null,ticket);
    }
  ], function( err, ticket){
    // Instead of returning to the caller, use your result here
    Ticket.remoteMethod('addComment', {
      http: { verb: 'post'},
      accepts: [
        {arg: 'ticket', type: 'whatever', value: ticket, description: 'an example of how you could return the result'},
        {arg: 'id', type: 'string', description: 'ticket id of the ticket the comment is to be added to'},
        {arg: 'comment', type: 'string', description: 'the comment body'},
        {arg: 'postedBy', type: 'string', description: 'Who posted the comment'}
      ],
      returns: {arg: 'comment', root: true, type: 'application/json'}
    });
    cb(); // <-- not forgetting the callback
  });

}

注意:在您的代码中,方法Ticket.findByIdticket.saveTicket.remoteMethod看起来是同步的。它们基本上违背了使用async.waterfall代码的目的:waterfall()中的每个函数都会阻止。除非您找到这些方法的异步版本,否则应完全删除异步。它只是让您的代码更复杂,没有任何显着的好处。