multiple, sequential fetch() Promise

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)
})

Leave a Comment