0

In this method, I have fetched a single API Request URL

function fetchData() {
  let url = 'http://127.0.0.1:8000/api/onlineUserData';
  fetch(url)
    .then(response => response.json())
    .then(data => {
      var users=data.users;
      console.log(data.users);
    });
}

and when I do console.log(data.users). Result or data will come like this.

[["Month", "Anam", "Panam", "duliyan"], ["Apr-16", 21, 26, 29], ["May-07", 0, 0, 5]] 

But now I want to fetch multiple URLs or API request and retrieve each API Request data at the same time. From this link I found the uses of promise but I don't how to use retrieve individual API Request Data How to fetch multiple API Request or URL at the same time?

function fetchData() {
  let urls = [
    'http://127.0.0.1:8000/api/onlineUserData',
    'http://127.0.0.1:8000/api/offlineUserData'
  ]
  let requests = urls.map(url => fetch(url));
  Promise.all(requests)
    .then(responses => responses.forEach(
      response => console.log(`${response.url}: ${response.status}`)
    ));
}
5
  • I can't figure out what you did with the answer. show your edited code. Commented Nov 2, 2020 at 7:23
  • @jinongun I just want to fetch two API URL and retrieve data from those URL and put in variable like var online-user and var offline-user.The main point of this i want to sum/add those data from two URL. Commented Nov 2, 2020 at 8:55
  • have you tried to see the console.log? what did you see? Commented Nov 2, 2020 at 10:38
  • 127.0.0.1:8000/api/userOnlineData 127.0.0.1:8000/api/userOfflineData 200 This in console.log() from your solution Commented Nov 2, 2020 at 12:10
  • Why don't you check response? I am not giving you the answer. I am just giving you advice. You should think about that. try console.log(response) instead of console.log(response.url); Commented Nov 2, 2020 at 12:35

1 Answer 1

2

Update your code to the following:

function fetchData(){
    let urls = [
     {
        url: 'http://127.0.0.1:8000/api/onlineUserData',
        type: 'offline'
     },
     {
        url: 'http://127.0.0.1:8000/api/offlineUserData',
        type: 'offline'
     },
     {
        url: 'http://127.0.0.1:8000/api/onlineUserData',
        type: 'online'
     },
    ];


    let requests = urls.map(item => fetch(item.url).then(response => response.json()));

    const resultData = { offline: [], online: [] };

    Promise.all(requests)
     .then(datas => {
       datas.forEach(
        (data, i) => {
         const url = urls[i];
         if (url.type === 'offline') 
             resultData.offine.push({...url, data});
         if (url.type === 'online') 
             resultData.online.push({...url, data});
        });

        console.log({resultData});
        /*
          {
             resultData: {
                offline: [
                  {
                    url: 'http://127.0.0.1:8000/api/oflineUserData',
                    type: 'offline',
                    data: [...]
                  },
                  {
                    url: 'http://127.0.0.1:8000/api/offlineUserData',
                    type: 'offline',
                    data: [...]
                  },
                ],
                online: [
                  {
                    url: 'http://127.0.0.1:8000/api/onlineUserData',
                    type: 'online',
                    data: [...]
                  },
                ]
             }
           }
        */
       }
  ));
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for your response but Suppose there two variable like online and offline and i want store each API Request data in variable respectively.like '127.0.0.1:8000/api/onlineUserData' data coming from this URL in online variable and 127.0.0.1:8000/api/offlineUserData in offline variable .
Can you update your answer with the snippet of how you want the result to be in the end
Let say i have two different URL '127.0.0.1:8000/api/onlineUserData', '127.0.0.1:8000/api/offlineUserData' I just only need to fetch them and retrive their data
I have updated the answer check the log about the output, see if it helps
yes it will work, make sure you define type: offline within urls
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.