let AllDatas = [],
AllDatasInError = [],
urls = [],
waitTime = 500,
start = 0,
step = 2,
position = 0,
nbRequest = 12;
AllDatas['total'] = 0;
let btn = document.getElementById('btn');
btn.addEventListener('click', function() {
batchProcessing(urls);
});
function createUrl() {
for (let i = 1; i <= nbRequest; i++) {
urls.push(`https://jsonplaceholder.typicode.com/photos/${i}`);
}
}
createUrl();
const waitFor = (ms) => new Promise(r => setTimeout(r, ms));
async function getData(links) {
try {
const res = await axios.all(links.map(link => axios.get(link)));
console.log('GetData Start time', new Date());
return res;
} catch (error) {
console.log('------------------------------------------');
console.log(error);
}
}
async function batchProcessing(urls) {
const len = urls.length / step;
console.time('batchProcessing');
console.log('batchProcessing Start time', new Date());
axios.interceptors.response.use(function (response) {
let responseData = response.data;
if (responseData.id) {
responseData['position'] = `${position}/${len}`;
addElement(responseData);
AllDatas['total'] += 1;
AllDatas.push(responseData);
}
console.log('responseData', responseData);
return responseData;
}, function (error) {
return Promise.reject(error);
});
for(let index = 0; index < len; index ++) {
var processingUrl = urls.slice(start, start + step);
start += step;
position += 1;
console.log('position', position + ' / ' + len);
console.log('processingUrl', processingUrl);
const res = await getData(processingUrl);
AllDatas = AllDatas.concat(res);
await waitFor(waitTime);
}
console.log('End timer', new Date());
console.timeEnd('getData');
}
function addElement (data) {
console.log('element added', data);
let mainDiv = document.getElementById("container");
let newDiv = document.createElement("div");
let newContent = `
<div class="card">
<div class="card-image">
<img src="${data.thumbnailUrl}">
<span class="card-title">
Element ${data.id}
</span>
</div>
<div class="card-content">
<p>${data.title}. (batch ${data.position})</p>
</div>
<div class="card-action">
<a href="${data.url}">See the source</a>
</div>
</div>`;
newDiv.innerHTML = newContent;
mainDiv.appendChild(newDiv);
}
View Compiled