[英]Promise does not resolve node.js
我試圖通過sendRequest
函數解決一些承諾,但它不起作用。
例如,如果我喚起sendRequest function
4次,所有的時間,我可以看到在控制台中打印的日志然后去resolve(data)
。 但是只有4次中的1次,程序進入sendRequest.then()
。
這是該文件的完整代碼。
var sendRequest = function(fileName){
return new Promise(function (resolve,reject) {
httpRequest(fileName).then(function (data) {
try{
if(.....){
if(.....){
var lastIndex = ....;}
if(.....){
var lastIndex = ....;}
str = fileName.substring(0, lastIndex);
if(.........){
sendRequest(str);}
else{
reject("this is the end");}
}
else{
console.log("end result" + JSON.stringify(data,null,4));
resolve(data);
//===== THIS RESOLVE DOES NOT WORK WHILE THE LOG PRINTS THE DATA =====//
}
}
catch(e){
resolve(data);
}
}).catch(function (err) {
reject(err);
});
});
};
這個文件從change-name.js
調用sendRequest
函數,然后在這里為該函數應用then方法。
fs.readdir(path,(err,files)=>{
if(err){
console.log(err);
return;
}
for(i=0;i<files.length;i++){
sendRequest(files[i]).then(function (data) {
console.log(data + "\n");
}).catch(function(err){
console.log("end Error is " + err + "\n");
});
console.log(files);
}
});
github鏈接是“ https://github.com/abhikulshrestha22/movierator ”。
任何幫助,將不勝感激。 謝謝
問題是你的一個分支再次調用sendRequest
而不是解析或拒絕,但是然后沒有使用遞歸調用返回的promise,所以你為外部調用創建的promise永遠不會被解析。 內部的一個(這就是你在控制台中看到消息的原因),但由於沒有任何東西正在使用這個承諾,你得不到你期望的結果。
您的代碼中根本不需要new Promise
。 httpRequest
已經給你一個承諾,你then
在其上處理程序創建一個承諾(如果返回的值)。 所以sendRequest
應該只返回調用then
在httpRequest
的promise上的結果,並在then
回調中,返回值來解析新的promise,或者拋出拒絕。 在您再次調用sendRequest
的分支中,返回它返回的promise; then
創建的then
將根據該承諾解決/拒絕:
var sendRequest = function(fileName) {
return httpRequest(fileName).then(function(data) {
if (condition1) {
if (condition2) {
var lastIndex = something;
}
if (condition3) {
var lastIndex = somethingElse;
}
str = fileName.substring(0, lastIndex);
if (condition4) {
return sendRequest(str);
}
else {
throw new Error("this is the end");
}
}
else {
console.log("end result" + JSON.stringify(data, null, 4));
return data;
}
});
};
這是一個實例,其中httpRequest
返回一個1-10的隨機數,如果該數字小於8,則再次調用sendRequest
; 在放棄之前它會嘗試三次:
function httpRequest() { return new Promise(function(resolve) { setTimeout(function() { resolve(Math.floor(Math.random() * 10) + 1); }, 500); }); } var sendRequest = function(fileName, retries) { if (typeof retries !== "number") { retries = 3; } return httpRequest(fileName).then(function(data) { console.log("`then` got: " + data); if (data > 7) { console.log("It's > 7, yay! We're done"); return data; } console.log("It's <= 7g"); if (--retries === 0) { console.log("Out of retries, rejecting"); throw new Error("out of retries"); } console.log("retrying (" + retries + ")"); return sendRequest(fileName, retries); }); }; document.getElementById("btn").addEventListener("click", function() { var btn = this; btn.disabled = true; console.log("Sending request"); sendRequest("foo") .then(function(data) { console.log("Request complete: " + data); }) .catch(function(err) { console.log("Request failed: " + err); // Because we don't throw here, it converts the rejection into a resolution... }) .then(function() { // ...which makes this kind of like a "finally" btn.disabled = false; }); }, false);
<input type="button" id="btn" value="Start">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.