2017-11-16 4 views
0

저는 아래와 같이 비동기식 폭포 예제를 따르려고했습니다.Nodeis Redis와 Waterfall이 다음 함수로 값을 전달하지 않습니다.

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

다음은 나의 코드 버전입니다.

var successfulRecords = 0 //successfulRecords holds the number of successfully processed rows 

     //Delete existing records for prepaid model before adding new records 
     async.waterfall([ 
      function(callback){ 
       redis.del("prepaid",function(err,data){ 
        if (err){ 
         logger.debug(err); 
        } 

        SCORES_CACHE = new Map(
         rows.map(function(row){ 
          redis.zadd("prepaid",row[1],row[0],function(err,response){ 

           if (err){ 
            logger.debug(err); 
           } 

           if (response==1){ 
            successfulRecords=successfulRecords+1; 
            console.log(successfulRecords); 
           } 
          }) 
         }) 
         ) 

       }) 
       callback(null,successfulRecords); 
      }, 

      function(arg1,callback){ 
       console.log('hello, waiting for arg1: ',arg1); 
      } 
      ], function(err,result){ 

      }); 

코드를 성공적으로 기록 카운터, 레디 스의 DB

  • 성공하면에이

    1. 삽입 기록처럼 작동 업데이트 할 예정
    2. 다음 함수에 successfulRecord 변수의 최종 카운트를 통과

    그러나이 결과가 계속 나타납니다.

    hello, waiting for arg1: 0 
    

    어떻게 해결할 수 있습니까? 몇 시간 동안이 일을 해왔다. 콜백을 올바르게 배치하지 않았습니까?

    미리 감사드립니다.

  • 답변

    0

    callback(null,successfulRecords);redis.del이 완료되기 전에 호출됩니다. 한 줄 위로 움직여 라.

    function(callback){ 
        redis.del("prepaid",function(err,data){ 
         if (err){ 
          logger.debug(err); 
         } 
    
         SCORES_CACHE = new Map(
          rows.map(function(row){ 
           redis.zadd("prepaid",row[1],row[0],function(err,response){ 
    
            if (err){ 
             logger.debug(err); 
            } 
    
            if (response==1){ 
             successfulRecords=successfulRecords+1; 
             console.log(successfulRecords); 
            } 
           }) 
          }) 
         ) 
         callback(null,successfulRecords); //One line up 
        }) 
    
    }