Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple, sequential fetch() Promise

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);
like image 383
FrancescoN Avatar asked Jun 26 '16 01:06

FrancescoN


People also ask

Does fetch use promise?

fetch() The global fetch() method starts the process of fetching a resource from the network, returning a promise which is fulfilled once the response is available. The promise resolves to the Response object representing the response to your request.

What is the difference between fetch and promise?

Fetch is another API that enables you to make network requests and REST service calls, similar to jQuery's AJAX methods or the native XMLHttpRequest . The main difference is that the Fetch API uses promises, which has a cleaner and more concise syntax that helps you avoid callback hell.

How use multiple fetch in JavaScript?

Now to do the parallel requests, we can pass all the fetch requests as an array to the Promise. all() mehtod like this, // 3 fetch requests for 3 endpints // and converting to JSON using the json() method const fetchReq1 = fetch( `https://jsonplaceholder.typicode.com/todos/1` ). then((res) => res.


1 Answers

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)
})
like image 60
guest271314 Avatar answered Oct 19 '22 22:10

guest271314