admin管理员组文章数量:1278825
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);
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);
Share
Improve this question
asked Jun 26, 2016 at 1:56
FrancescoNFrancescoN
2,18612 gold badges36 silver badges48 bronze badges
13
- What is expected behavior and overall goal? – charlietfl Commented Jun 26, 2016 at 2:12
-
How many total requests are expected to be processed?
$q.when()
does not appear to be necessary. Why do you callgetItems(next_json_url)
outside of.then()
withingetItems()
call? – guest271314 Commented Jun 26, 2016 at 2:12 - what is the base case? when will you stop fetching data? – Zohaib Ijaz Commented Jun 26, 2016 at 2:12
- i have to download 1 json at a time: the first has a reference for the next one, and so on @guest27131 as many as I want.. 1-40. I'll stop when there is no more reference, in current json, for the next json – FrancescoN Commented Jun 26, 2016 at 2:21
- 1 Might consider making sure you have a master nav map and work from that instead. Would make it easier to handle errors since one error will break your chain and map would give you a place to track updates from – charlietfl Commented Jun 26, 2016 at 3:44
1 Answer
Reset to default 10You can use recursion
function fetchNextJson(json_url) {
return fetch(json_url, {
method: 'get'
})
.then(function(response) {
return response.json();
})
.then(function(json) {
results.push(json);
return json.Pagination.NextPage.Href
? fetchNextJson(json.Pagination.NextPage.Href)
: results
})
.catch(function(err) {
console.log('error: ' + error);
});
}
var next_json_url = 'http://localhost:3000/one';
var results = [];
fetchNextJson(json_url).then(function(res) {
console.log(res)
})
本文标签: javascriptmultiplesequential fetch() PromiseStack Overflow
版权声明:本文标题:javascript - multiple, sequential fetch() Promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741252996a2366142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论