Promises

by Aashish Manandhar

JavaScript

const superHeroes = ['Batman', 'Superman', 'WonderWoman', 'Flash', 'Cyborg', 'Aquaman', 'Green Lantern', 'Martian Manhunter'];
const ajaxRequest = (param, callback) => {
  let xhr = new XMLHttpRequest();
  xhr.addEventListener('load', () => {
    callback(xhr.responseText);
  });
  xhr.open('GET', `https://httpbin.org/get?param=${param}`);
  xhr.send();
}
const completedFetchingData = () => {
  console.log('\nJust completed fetching the data');
}
superHeroes.forEach((superHero, index) => {
  ajaxRequest(superHero, (response) => {
    const responseObj = JSON.parse(response);
    document.write('\nResponse', responseObj.args.param);
    if (index === superHeroes.length - 1) {
      completedFetchingData();
    }
  });
});