JSFiddle - React, Tailwind, and code Playground

by infous

JavaScript

const urls = [
	'https://jsonplaceholder.typicode.com/todos/1',
  'http://aaa.two',
  'http://aaa.three'
];

function fetchUrl(url) {
  return fetch(url);
}

function fetchUrls(urls) {
  const url = urls.shift();
  console.log('AAA', `fetching ${url}`);

  fetchUrl(url)
    .then((response) => {
      console.log('AAA', `getting response from ${url}`);
      if (response.ok && urls.length) {
        console.log('AAA', `response is OK from ${url}`);
        return response.json();
      }
      console.log('AAA', `no response from ${url}`);
    })
    .then((result) => {
      console.log('AAA', `result is ${result}`);
      fetchUrls(urls);
    })
    .catch((reason) => {
      console.log('AAA', `error ${reson}`);
    });
}

fetchUrls([...urls]);