Я переписываю приложение, которое выполняет вызовы API REST в пакетном режиме, например, всего 500 вызовов, выполняемых по 10 за раз. Мне нужна помощь в понижении моей функции js, которая использует функции ES6 +, до эквивалента ES5 (практически без функций со стрелками или асинхронного / ожидающего).
В моем исходном приложении, которое использовалось в средах, поддерживающих функции ES6 + (стрелка functions, async / await et c.) моя рабочая функция была следующей:
Исходная функция:
// Async function to process rest calls in batches
const searchIssues = async (restCalls, batchSize, loadingText) => {
const restCallsLength = restCalls.length;
var issues = [];
for (let i = 0; i < restCallsLength; i += batchSize) {
//create batch of requests
var requests = restCalls.slice(i, i + batchSize).map((restCall) => {
return fetch(restCall)
.then(function(fieldResponse) {
return fieldResponse.json()
})
.then(d => {
response = d.issues;
//for each issue in respose, push to issues array
response.forEach(issue => {
issue.fields.key = issue.key
issues.push(issue.fields)
});
})
})
// await will force current batch to resolve, then start the next iteration.
await Promise.all(requests)
.catch(e => console.log(`Error in processing batch ${i} - ${e}`)) // Catch the error.
//update loading text
d3.selectAll(".loading-text")
.text(loadingText + ": " + parseInt((i / restCallsLength) * 100) + "%")
}
//loading is done, set to 100%
d3.selectAll(".loading-text")
.text(loadingText + ": 100%")
return issues
}
Код, который я написал до сих пор например, правильно объединяет остальные вызовы в первой группе из 10, но я, похоже, запутался в разрешении Обещания, поэтому for-l oop может продолжить итерацию.
Моя переписываемая функция незавершенного производства:
//Async function process rest calls in batches
function searchIssues(restCalls, batchSize, loadingText) {
const restCallsLength = restCalls.length;
var issues = [];
for (var i = 0; i < restCallsLength; i += batchSize) {
//create batch of requests
var requests = restCalls.slice(i, i + batchSize).map(function(restCall) {
return fetch(restCall)
.then(function(fieldResponse) {
return fieldResponse.json()
})
.then(function(data) {
console.log(data)
response = data.issues;
//for each issue in respose, push to issues array
response.forEach(function(issue) {
issue.fields.key = issue.key
issues.push(issue.fields)
});
})
})
//await will force current batch to resolve, then start the next iteration.
return Promise.resolve().then(function() {
console.log(i)
return Promise.all(requests);
}).then(function() {
d3.selectAll(".loading-text")
.text(loadingText + ": " + parseInt((i / restCallsLength) * 100) + "%")
});
//.catch(e => console.log(`Error in processing batch ${i} - ${e}`)) // Catch the error.
}
//loading is done, set to 100%
d3.selectAll(".loading-text")
.text(loadingText + ": 100%")
return issues
}
Мой вопрос: как только мои 10 restCalls будут выполнены, как я могу тогда правильно выполнить Обещание и продолжить итерацию по for-l oop?
Для справки, я попытался скомпилировать оригинальную функцию с помощью Babel, но она не скомпилировалась в моем приложении Maven, поэтому переписывал с нуля.