多个顺序fetch()承诺
我必须做一个fetch()承诺序列:一次我只有1个网址,这意味着只有1个fetch()诺言。每次我收到一个json时,它都会包含另一个json的网址,因此我必须做出另一个fetch()承诺。
我可以使用多个promise,但是在这种情况下,我做不到Promise.all(),因为我没有所有的URL,只有一个。
这个例子不起作用,一切都冻结了。
function fetchNextJson(json_url) {
return fetch(json_url, {
method: 'get'
})
.then(function(response) {
return response.json();
})
.then(function(json) {
console.log(json);
return json;
})
.catch(function(err) {
console.log('error: ' + error);
});}function getItems(next_json_url) {
if (!(next_json_url)) return;
get_items = fetchNextJson(next_json_url);
interval = $q.when(get_items).then(function(response) {
console.log(response);
next_json_url = response.Pagination.NextPage.Href;
});
getItems(next_json_url);}var next_json_url = 'http://localhost:3000/one';getItems(next_json_url);蝴蝶刀刀
开满天机
阿晨1998
相关分类