我正在使用具有以下步骤的程序:
- 从数据库中获取用户数据(它有超过 2000 个用户
- 获取所有用户的角色列表(每个用户可能有角色)
- 向具有更多角色的用户发送邮件
同步运行它们将花费大量时间。需要一个多小时。我正在进行多个 axios 调用以从服务器获取数据。我希望在进入下一个触发邮件的方法之前等待所有这些作业完成的逻辑。
我试过了Promise.all
,但我只能传递一个同名的参数。就我而言,我正在使用相同的变量名在 for 循环中调用 axios。
const axios = require("axios");
const https = require("https");
async function fetch_finaldata(req, user_records, token) {
try {
// get list of available users from system
let res_usercount = await getuser_count(req);
do {
// get users 200 each to save time
let res_user = getdemousers(req, user_records);
// get list of roles for each user
let res_catalogs = getusercatalogs(req, user_records, res1);
// increase the counter to read next 200 records
skip = skip + 200;
} while (skip < res_usercount.data);
// get user count
async function getuser_count(req) {
let usercount = {
method: "GET",
url: "*****************",
headers: {
Authorization: "***************",
},
};
let usercount_res = axios(usercount);
return usercount_res;
}
// get users
async function getdemousers(req, user_records) {
let demoUserParameters = {
method: "GET",
url: `*******$skip=${skip}&$top=200&$orderby=UserName asc&$select=UserID,UserName,FirstName,LastName,EmailAddress,to_AssignedBusinessRoles&$expand=to_AssignedBusinessRoles&saml2=disabled&$format=json`,
headers: {
Authorization: "**********************",
},
};
let demoUsersResponse = await axios(demoUserParameters);
return demoUsersResponse;
}
//get user role information
async function getusercatalogs(req, user_records, res_user) {
try {
// loop through each user and fetch relavant details
for (let i = 0; i < filterusers.length; i++) {
// console.log(filterusers[i]).UserName;
let user = filterusers[i];
let roles = user.to_AssignedBusinessRoles.results;
try {
for (let k = 0; k < roles.length; k++) {
let catalogparams = {
method: "GET",
url: `******************`,
headers: {
Authorization: "************",
},
};
try {
// make an axios call to fetch relevant details
getcatalogs = axios(catalogparams);
getcatalogs.then((getcatalogs) => {
console.log("Catalog stage reached ");
console.log(getcatalogs.data.d.results);
let catalogs = getcatalogs.data.d.results;
try {
for (let t = 0; t < catalogs.length; t++) {
let tileparams = {
method: "GET",
url: `************************`,
headers: {
Authorization: "**************************",
},
};
try {
// make an axios call to fetch relevant details
let tiles_res = axios(tileparams);
tiles_res.then((tiles_res) => {
if (tiles_res.data > 0) {
console.log("Tile count is " + tiles_res.data);
}
});
} catch (error) {
console.log(error.message);
}
}
} finally {
}
});
} catch (error) {
console.log(error.message);
}
}
} catch (error) {
console.log(error.message);
}
}
}
}
} catch (error) {
console.log(error.message);
}
}
exports.fetch_finaldata = fetch_finaldata;
你能帮我解决这个问题吗?