我需要继续调用远程API,直到得到我需要的响应,并且我想在node.js中使用官方A + promises。同步psudo代码:
params = { remote api call params }
while (true) {
result = callRemoteApi(params)
if isGood(result) {
onSuccess(result)
break
}
params = modify(params)
}
我正在使用request-promise lib来获取请求,因此结果可能是这样的:
new Promise(function (resolve, reject) {
var task = request({params})
.then(function (result) {
if (isGood(result)) {
resolve(result);
} else {
task = request({new params}).then(this_function);
}
});
P.S。这与https://stackoverflow.com/a/17238793/177275非常相似,但我想要一个非基于q的实现。
答案 0 :(得分:6)
这样的事情应该运作良好:
var remoteApiCall = function(num){
// fake function to resolve promise if random number in range
return new Promise(function(resolve, reject){
return ((Math.random()*10) < num)
? resolve(true)
: reject(false);
})
}
function getUntil(num){
return remoteApiCall(num).then(function(result){
if (result) {
return result
} else {
// call again until you get valid response
return getUntil(num)
}
})
}
getUntil(num).then(...)
答案 1 :(得分:2)
以下解决方案解决了一些具体问题:
params = modify(params)
问题可以分为两个部分:
onSuccess
挂钩到它)转发器看起来像这样:
function repeatUntilSuccess(promiseProvider) {
return new Promise(function (resolve, reject) {
var counter = 0;
function run(failedResult) {
var p = promiseProvider(failedResult, counter++);
if (p instanceof Promise) {
p.then(resolve).catch(run);
} else {
reject(p);
}
}
run();
});
}
“循环”发生在这一行:p.then(resolve).catch(run);
。转发器继续调用promise提供程序,直到它返回的promise解析(在这种情况下转发器解析)或,直到它不再提供promise(在这种情况下转发器拒绝)。
承诺提供者可以是任何返回承诺的function(previousResult, counter)
(如果您希望停止循环,则不是。)
var params = {/* ... */};
function nextRequest(previousResult, counter) {
if (counter >= 10) return "too many attempts";
if (previousResult) params = modify(params);
return apiRequest(params);
}
(此设置假定apiRequest()
返回承诺)
现在你可以这样做:
repeatUntilSuccess(nextRequest).then(onSuccess).catch(onError);
由于您的问题包括在promise中包装HTTP请求的附带任务:
function apiRequest(params) {
return new Promise(function (resolve, reject) {
return request(params).then(function (result) {
if (isGood(result)) {
resolve(result);
} else {
reject(result);
}
});
});
}
打开浏览器控制台并运行以下代码段以查看其实际效果。
function repeatUntilSuccess(promiseProvider) {
return new Promise(function (resolve, reject) {
var counter = 0;
function run(failedResult) {
var p = promiseProvider(failedResult, counter++);
if (p instanceof Promise) {
p.then(resolve).catch(run);
} else {
reject(p);
}
}
run();
});
}
// mockup promise that resoves or rejects randomly after a timeout
function randomPromise(num){
return new Promise(function(resolve, reject){
setTimeout(function () {
var randomNum = Math.floor(Math.random() * num * 10);
if (randomNum < num) {
resolve(randomNum);
} else {
reject(randomNum);
}
}, 300);
});
}
// promise provider
function nextPromise(prev, i) {
if (prev) console.info("failed attempt #" + i + ": " + prev);
if (i >= 5) return "too many attempts:" + i;
return randomPromise(100);
}
// run it!
repeatUntilSuccess(nextPromise).then(function (result) {
console.log("success", result);
}).catch(function (result) {
console.log("failed", result);
});