I am trying to process concurrent AJAX requests with vanilla JavaScript. Below is the sample code I've been working with
function getInfoViaConcurrency(ids) {
let infoPromiseArr = ids.map(id=> { infoPromiseArr.push(getInfoViaAjax(id)); });
return Promise.all(infoPromiseArr).then(values => { return values; });
}
function getInfoViaAjax(id) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', `API URL`);
xhr.onload = function() {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
}
else {
reject(xhr.status);
}
};
xhr.send();
})
}
Does using promise.all suffice to promote concurrency? can someone suggest any improvements?