2017-12-26 25 views
0

최근 버전의 async waterfall에서는 cb를 더 이상 사용할 수 없다는 것을 알고 있습니다.loopbackJS에 노드 async 폭포가있는 결과를 반환

그렇다면 원격 메소드에 어떻게 응답합니까? 나는 이것이 어디에서나 설명되는 것을 발견 할 수 없다.

비동기 문서의 예제를 사용하려면.

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

답변

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); 
     } 
    } 
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.findById, ticket.saveTicket.remoteMethod 모양 동기에. 기본적으로 async.waterfall을 사용하여 코드를 작성하는 목적을 무효화합니다. waterfall() 내의 각 함수는 차단됩니다. 이러한 메소드의 비동기 버전을 발견하지 못하면 모두 비동기를 제거해야합니다. 특별한 이점없이 코드를 복잡하게 만드는 것입니다.

+0

안녕하세요 flaviodesousa .. 네, 뭔가를 반환해야합니다. 그것은 티켓을 업데이 트하는 API 호출입니다 .. 그리고 티켓을 반환해야합니다. 최종 콜백에 배치해야 함을 의미하는 것을 설명 할 수 있습니까? 그 일을하고 있지만 API 호출로 돌아가는 것 같지 않습니다. 내가 티켓을 응답을 전달하려고하는 실제 이벤트로 업데이트합니다. 감사합니다 – Jason

+0

비동기 코드가 어떻게 보일지에 대한 예제를 추가했습니다. API에는 몇 가지 문제가 있지만 비동기로 보입니다. – flaviodesousa