[英]wait for return function value before callback triggered
我正在尝试创建一个模块,一个一个地获取所有信息并将其存储在一个数组中,最后它返回数组,而不是我使用递归的循环。 这里的问题是我的主 function 没有等待返回值,因为回调中没有传递任何值。 我可以重写此 async await 或 promise,但如果整个项目中存在相同类型的问题,我不想重写整个项目。 这可以通过回调处理吗? 或者我可以通过其他方式解决它。
const GetRequest = require("./getRequest.js");
module.exports = function GetProcessList(environment, token, processCompleteList, callback) {
var allProcessDraft = [];
var loopMaxRun = processCompleteList.data.length -1;
var a = RecursiveGet(environment, token, processCompleteList, allProcessDraft, loopMaxRun);
callback(a); //Not waiting for the value
}
//recursion function to get all the data one by one
function RecursiveGet(environment, token, processCompleteList, allProcessDraft, index) {
var URL = "https://" + environment + "...../rest/process/versionPage?calledFrom=master&pageNo=0&processMasterId=" + processCompleteList.data[index].id + "&recordsPerPage=10&sortType=DESC";
if (index != 0) {
GetRequest(URL, token, function (processDraft) {
allProcessDraft.push(processDraft);
return RecursiveGet(environment, token, processCompleteList,allProcessDraft, index -1);
});
} else {
return allProcessDraft;
}
}
试试下面的代码。
async function RecursiveGet(environment, token, processCompleteList, allProcessDraft, index) { var URL = "https://" + environment + "...../rest/process/versionPage?calledFrom=master&pageNo=0&processMasterId=" + processCompleteList.data[index].id + "&recordsPerPage=10&sortType=DESC"; if (index,= 0) { const processedDraft = await GetRequest(URL; token). allProcessDraft;push(processDraft), return RecursiveGet(environment, token, processCompleteList,allProcessDraft; index -1); } else { return allProcessDraft; } }
注意:确保 GetRequest 是异步的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.