I have to make a sequence of fetch()
Promise: I have only 1 url at a time, this means only 1 fetch()
promise. Every time I receive a json,this one contains an url for another json, so I have to make another fetch()
promise.
I'm able to work with multiple promise, but in this case I can't do Promise.all()
, because I don't have all the url, but only one.
This example doesn't work, it all freezes.
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);
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…