Javascript 将异步/等待函数转换为ES5等效函数
我正在重写一个成批执行RESTAPI调用的应用程序,例如每次执行10个调用,总共执行500个调用。我需要帮助将使用ES6+函数的js函数降级为ES5等效函数(基本上没有箭头函数或async/await) 在支持ES6+功能(箭头功能、异步/等待等)的环境中使用的原始应用程序中,我的工作功能如下: 原始功能:Javascript 将异步/等待函数转换为ES5等效函数,javascript,maven,Javascript,Maven,我正在重写一个成批执行RESTAPI调用的应用程序,例如每次执行10个调用,总共执行500个调用。我需要帮助将使用ES6+函数的js函数降级为ES5等效函数(基本上没有箭头函数或async/await) 在支持ES6+功能(箭头功能、异步/等待等)的环境中使用的原始应用程序中,我的工作功能如下: 原始功能: // Async function to process rest calls in batches const searchIssues = async (restCalls, batch
// 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
}
//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
}
//用于批量处理rest调用的异步函数
const searchIssues=async(restCalls、batchSize、loadingText)=>{
const restCallsLength=restCalls.length;
风险值问题=[];
for(设i=0;i{
返回获取(restCall)
.然后(功能(现场响应){
返回fieldResponse.json()
})
.然后(d=>{
答复=d.问题;
//对于respose中的每个问题,推送到问题数组
response.forEach(问题=>{
issue.fields.key=issue.key
问题推送(问题字段)
});
})
})
//wait将强制当前批处理进行解析,然后开始下一次迭代。
等待承诺。所有(请求)
.catch(e=>console.log(`Error in processing batch${i}-${e}`))//捕获错误。
//更新加载文本
d3.选择全部(“加载文本”)
.text(loadingText+“:“+parseInt((i/restCallsLength)*100)+“%”)
}
//加载完成,设置为100%
d3.选择全部(“加载文本”)
.text(加载文本+“:100%”)
退货问题
}
例如,到目前为止,我编写的代码正确地对第一组10中的rest调用进行了批处理,但我似乎在解决承诺时遇到了问题,因此for循环可以继续迭代
我正在进行的工作重写功能:
// 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
}
//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
}
//异步函数批量处理rest调用
函数搜索问题(restCalls、batchSize、loadingText){
const restCallsLength=restCalls.length;
风险值问题=[];
对于(变量i=0;iconsole.log(`Error in processing batch${i}-${e}`))//捕获错误。
}
//加载完成,设置为100%
d3.选择全部(“加载文本”)
.text(加载文本+“:100%”)
退货问题
}
我的问题是,一旦我的10个rest调用完成,我如何才能正确地解决承诺并继续迭代for循环
作为参考,我曾尝试使用Babel编译原始函数,但它无法在我的Maven应用程序中编译,因此重新编写。如果没有
async/await
,则无法暂停循环。但是,您可以通过使用递归函数来重现这种行为,在每批10次之后调用自己。沿着这些思路(未经测试):
//用于批量处理rest调用的异步函数
函数搜索问题(restCalls、batchSize、loadingText){
var restCallsLength=restCalls.length,
问题=[],
i=0;
返回新承诺(功能(解决、拒绝){
(函数循环(){
如果(i{
issue.fields.key=issue.key;
问题.推送(问题.字段);
});
});
});
返回承诺。所有(请求)
.catch(函数(e){
log(`Error in processing batch${i}-${e}`);
})
.然后(函数(){
//不管它是否失败,转到下一个迭代
d3.选择全部(“加载文本”)。文本(
loadingText+“:“+parseInt((i/restCallsLength)*100)+”%
);
i+=批量大小;
loop();
});
}否则{
//加载完成,设置为100%
d3.选择全部(“.loading text”)。文本(loadingText+”:100%);
解决(问题);//解决外部承诺
}
})();
});
}
你说的是什么意思