多個順序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);
多個順序fetch()承諾
叮當貓咪
2019-09-26 15:03:11