2017-12-14 14 views
0

저는 로컬 DB에 데이터를 동기화 할 수있는 API를 사용하고 있습니다. 동기화 일괄 처리가 데이터 전송을 시작할 준비가 될 때까지 재귀 적으로 호출하는 syncReady API가 있습니다. 재귀가 제대로 작동하고 .then 콜백이 호출되지만 resolve 함수는 응답을 해결하지 않습니다. Node.JS 재귀 적 약속이 해결되지 않음

const request = require('request-promise'); 
const config = require('../Configs/config.json'); 

function Sync(){} 

Sync.prototype.syncReady = function (token, batchID) { 
    return new Promise((res, rej) => { 
     config.headers.Get.authorization = `bearer ${token}`; 
     config.properties.SyncPrep.id = batchID; 
     request({url: config.url.SyncReady, method: config.Method.Get, headers: config.headers.Get, qs: config.properties.SyncPrep}) 
      .then((response) => { 
       console.log(`The Response: ${response}`); 
       res(response); 
      }, (error) => { 
       console.log(error.statusCode); 
       if(error.statusCode === 497){ 
        this.syncReady(token, batchID); 
       } else rej(error); 
      } 
     ); 
    }); 
}; 

내가 로그인 한 497과는 "응답 얻을 : {"pagesTotal을 "0}"응답 그러나 입술 (응답) 결코 체인 아래로 응답을 보내지 않습니다. 전체 체인을 따라 console.log 메시지를 추가했으며 체인의 아래쪽에있는 .then 함수 중 아무 것도 실행되지 않습니다.

나는 이것을 충분히 설명했으면 좋겠다 .--). 약속이 해결되지 않는 이유는 무엇입니까?

감사합니다.

답변

2

먼저 약속을 반환하는 내용을 new Promise으로 마무리 할 필요가 없습니다. 둘째, 오류 경우에 대해서는 497 인 경우 약속을 해결하지 못합니다.

const request = require('request-promise'); 
 
const config = require('../Configs/config.json'); 
 

 
function Sync(){} 
 

 
Sync.prototype.syncReady = function (token, batchID) { 
 
     config.headers.Get.authorization = `bearer ${token}`; 
 
     config.properties.SyncPrep.id = batchID; 
 
     return request({url: config.url.SyncReady, method: config.Method.Get, headers: config.headers.Get, qs: config.properties.SyncPrep}) 
 
      .then((response) => { 
 
       console.log(`The Response: ${response}`); 
 
       return response; 
 
      }) 
 
      .catch((error) => { 
 
       console.log(error.statusCode); 
 
       if(error.statusCode === 497){ 
 
        return this.syncReady(token, batchID); 
 
       } else { 
 
        throw error; 
 
       } 
 
      }) 
 
    ); 
 
};

아마 위의 같은 것을 대신 당신을 위해 작동합니다. 어쩌면 대신 위에 시도하십시오. 일반적으로 대략 Promise을 반환하는 것이 좋습니다.

+0

감사합니다. 497을 해결하는 것이 체인 해결을위한 열쇠였습니다. res (this.syncReady (토큰, batchID)); – DrFiasco

+0

정말, 나는 약속을 그 자체로 되 돌리는 것이기 때문에 약속을 다른 약속으로 포장하는 것을 권장합니다. – Goblinlord