7

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); 
13
  • What is expected behavior and overall goal? 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 call getItems(next_json_url) outside of .then() within getItems() call? Commented Jun 26, 2016 at 2:12
  • what is the base case? when will you stop fetching data? 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 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 Commented Jun 26, 2016 at 3:44

1 Answer 1

10

You 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) }) 
Sign up to request clarification or add additional context in comments.

15 Comments

Made simplified demo using a counter instead of having a new url to return plnkr.co/edit/5boVimpYnoE1o41FXZGG?p=preview
@charlietfl "conclusion is constantly adding then() to chain until finally a non promise is returned." Any result returned from .then() chained to fetchNextJson should be promise after first call; .then() either recursively calls fetchNextJson or returns accumulated results, or other value, as a promise value
@charlietfl .catch() should stop recursion, unless fetchNextJson is called within .catch(). One approach could be to handle error, then return fetchNextJson at .then() chained to .catch() or in .catch(); though given present requirement is to call next fetchNextJson with results from current fetchNextJson there would not appear to be any further tasks to perform - unless url is provided from an optional source
Had not considered such a case. Yes, could call fetchNextJson() with current url within .catch() if error occurred, to try to continue recursive chain; store, limit number of reties. Though, again, given present requirement, there would not be a source for next url. If requirement included seaparate array of urls, could iterate entire array, whether errors were caught, handled, or successful response returned. Once error handled by .catch(), can return promise to chained .then() that should not be an error within .then(); could continue with recursive call to function
Oh I agree. Just thinking out of the box a bit.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.