如何在到达函数末尾之前等待所有超级代理调用完成
How to wait for all superagent calls to complete before reaching the end of the function
我正在尝试进行多个 api 调用,这些调用是根据某些输入动态创建的,问题是该方法在所有 Ajax 调用完成之前结束。在实现所有结果后,我需要触发一个附加函数来使用 Api 调用的结果 json 值。
我很确定,我可能错误地使用了 promises,或者对它们感到困惑,我仍在努力掌握它们,但无法解决这个问题。非常感谢所有帮助。
fields() 函数应该 return 包含类似于以下 2 个对象的对象数组。
{"title":"Businessunit",
"content":
{"filter":
{
"fields":{"businessunitid":true,"settingsid":true,"archived":true}
}
}
}
{"title":"Address",
"content":
{"filter":
{
"fields":{"addressid":true}
}
}
}
function Export()
{
queryGroup = fields();
const response = async.map(queryGroup, a=>ApiCall(a),
function(err, results) {
return results;
});
return response;
}
async function ApiCall(lookup)
{
console.log("LOOKUP: " + JSON.stringify(lookup));
var table = new Object();
superagent
.get(document.getElementById("ApiEndPoint").value+'/api/'+document.getElementById("ApiKey").value+'/'+lookup.title)
.query(lookup.content)
.set('accept', 'json')
.then(res =>
{
table["Body"] = res.body
table["Name"] = lookup.title;
console.log("completed: "+ lookup.title);
return table;
}).then(table => {return Promise.resolve(table);});
}
一旦所有 apiCall
都处理完毕,您的 Export
应该 return 一个 Promise.all
。在 apiCall
中的最后一个 .then
中创建要 returned 的对象,以便 Promise 链解析为该对象。
因为 export
是保留关键字,所以为该函数使用不同的名称可能是个好主意,例如 getAllFieldInfo
或类似的名称:
function getAllFieldInfo() {
return Promise.all(fields().map(apiCall));
}
function apiCall(lookup) {
const superagentGetParam = document.getElementById("ApiEndPoint").value + '/api/' + document.getElementById("ApiKey").value + '/' + lookup.title;
return superagent
.get(superagentGetParam)
.query(lookup.content)
.set('accept', 'json')
.then(res => ({
Body: res.body,
Name: lookup.title
}));
}
我正在尝试进行多个 api 调用,这些调用是根据某些输入动态创建的,问题是该方法在所有 Ajax 调用完成之前结束。在实现所有结果后,我需要触发一个附加函数来使用 Api 调用的结果 json 值。
我很确定,我可能错误地使用了 promises,或者对它们感到困惑,我仍在努力掌握它们,但无法解决这个问题。非常感谢所有帮助。
fields() 函数应该 return 包含类似于以下 2 个对象的对象数组。
{"title":"Businessunit",
"content":
{"filter":
{
"fields":{"businessunitid":true,"settingsid":true,"archived":true}
}
}
}
{"title":"Address",
"content":
{"filter":
{
"fields":{"addressid":true}
}
}
}
function Export()
{
queryGroup = fields();
const response = async.map(queryGroup, a=>ApiCall(a),
function(err, results) {
return results;
});
return response;
}
async function ApiCall(lookup)
{
console.log("LOOKUP: " + JSON.stringify(lookup));
var table = new Object();
superagent
.get(document.getElementById("ApiEndPoint").value+'/api/'+document.getElementById("ApiKey").value+'/'+lookup.title)
.query(lookup.content)
.set('accept', 'json')
.then(res =>
{
table["Body"] = res.body
table["Name"] = lookup.title;
console.log("completed: "+ lookup.title);
return table;
}).then(table => {return Promise.resolve(table);});
}
一旦所有 apiCall
都处理完毕,您的 Export
应该 return 一个 Promise.all
。在 apiCall
中的最后一个 .then
中创建要 returned 的对象,以便 Promise 链解析为该对象。
因为 export
是保留关键字,所以为该函数使用不同的名称可能是个好主意,例如 getAllFieldInfo
或类似的名称:
function getAllFieldInfo() {
return Promise.all(fields().map(apiCall));
}
function apiCall(lookup) {
const superagentGetParam = document.getElementById("ApiEndPoint").value + '/api/' + document.getElementById("ApiKey").value + '/' + lookup.title;
return superagent
.get(superagentGetParam)
.query(lookup.content)
.set('accept', 'json')
.then(res => ({
Body: res.body,
Name: lookup.title
}));
}