我想从一个 url 发出 HTTP GET 请求,它将包含下一个网页的 url。我必须继续这个过程,直到得到一个空的“下一个”网址。
我的代码如下:
Parse.Cloud.define("myFunc", fucntion (request, response){
Parse.Cloud.httpRequest({
url: fb_url
}).then(function(httpResponse) {
next_url = httpResponse.data.next_url;
/******************/
// code to make another HttpRequest with next_url and iteratively
// doing it till next_url is null
response.success(httpResponse.text);
}, function(httpResponse) {
response.error("error " + httpResponse);
}
});
我尝试了很多不同的方法,但都是徒劳。谁能告诉我如何使用 next_url 创建另一个 HttpRequest 并继续执行直到 next_url 为 null。
最佳答案
将 http 调用包装在可以递归调用的函数中。这将返回一系列发出请求的 promise ,直到返回 null。
function keepGetting(url) {
return Parse.Cloud.httpRequest({ url:url }).then(function(httpResponse) {
nextUrl = httpResponse.data.nextUrl;
return (nextUrl === null)? httpResponse : keepGetting(nextUrl);
});
}
Parse.Cloud.define("myFunc", fucntion (request, response){
// initialize fb_url somehow
keepGetting(fb_url).then(function(result) {
response.success(result);
}, function(error) {
response.error(error);
});
});
(小心,如果服务花费太长时间或在 null 之前返回太多结果,您的解析调用将超时)
关于javascript - 如何在 Parse Cloud 中使用分页游标创建多个 HttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005474/