Promise.all与axios/node.js
我有点被困在这里,试图等待两个或更多axios承诺进一步处理它们的结果。控制台日志(“测试”);在其他方法调用完全执行之前执行。我觉得我如何设置递归解析是不正确的,但不确定要调整什么。这里有人有主意吗?非常感谢你 输出--- 索引--- 跳马课---Promise.all与axios/node.js,node.js,promise,axios,Node.js,Promise,Axios,我有点被困在这里,试图等待两个或更多axios承诺进一步处理它们的结果。控制台日志(“测试”);在其他方法调用完全执行之前执行。我觉得我如何设置递归解析是不正确的,但不确定要调整什么。这里有人有主意吗?非常感谢你 输出--- 索引--- 跳马课--- 异步登录(){ 返回此参数。axiosInstance .post('/api/v20.2/auth',username='+this.username+'&password='+this.password,this.config) 。然后(响应=
异步登录(){
返回此参数。axiosInstance
.post('/api/v20.2/auth',username='+this.username+'&password='+this.password,this.config)
。然后(响应=>{
if(response.data.responseStatus=“成功”){
this.axiostation.defaults.headers.common['Authorization']=response.data.sessionId;
}
})
.catch(error=>console.error(error));
}
异步收集AuditTrailHistory(audittrail、startDate、endDate){
//检查有效登录名
if(this.isLoggedIn()){
返回此参数。axiosInstance
.get('/api/v20.2/audittrail/'+audittrail+'?开始日期='+startDate+'&end日期='+endDate+'&limit=1',this.config)
。然后(响应=>{
这个.parseAuditTrailHistoryPage(audittrail,startDate,endDate,0,response.data.responseDetails.total,1000);
})
.catch(error=>console.error(error));
}否则{
返回this.login()
。然后((响应)=>{
此.collectAuditTrailHistory(audittrail、startDate、endDate);
});
}
}
异步collectGroupMembers(groudId){
//检查有效登录名
if(this.isLoggedIn()){
返回此参数。axiosInstance
.get('/api/v20.2/objects/groups/'+groudId,this.config)
。然后(响应=>{
对于(设i=0;iconsole.error(error));
}否则{
返回this.login()
。然后((响应)=>{
返回此.collectGroupMembers(groudId);
});
}
}
异步收集用户名(groudId,userId){
//检查有效登录名
if(this.isLoggedIn()){
返回此参数。axiosInstance
.get('/api/v20.2/objects/users/'+userId,this.config)
。然后(响应=>{
log(新日期()+“-”+groudId+“-”+userId);
this.groupMembers.push([groudId,response.data.users[0].user.user\u name\u v]);
})
.catch(error=>console.error(error));
}否则{
返回this.login()
。然后((响应)=>{
返回此.collectUserName(groudId,userId);
});
}
}
伊斯洛格丁(){
如果(!this.axioInstance.defaults.headers.common['Authorization']| |!this.lastTxnDateTime)
返回false;
否则{
让diff=Math.abs(new Date()-this.lasttxndatime);
如果(数学楼层((差值/1000)/60)<15){
返回true;
}
否则{
返回false;
}
}
}
异步parseAuditTrailHistoryPage(audittrail、startDate、endDate、偏移量、总计、限制){
日志(新日期()+“-”+开始日期+“-”+结束日期+“-”+偏移量+“-”+总计+“-”+限制);
返回此参数。axiosInstance
.get('/api/v20.2/audittrail/'+audittrail+'?开始日期='+startDate+'&结束日期='+endDate+'&限制='+limit+'&偏移='+offset,this.config)
。然后(响应=>{
如果((偏移+限制)<总计){
this.auditTrailHistory=this.auditTrailHistory.concat(response.data.data);
此.parseAuditTrailHistoryPage(audittrail、startDate、endDate,(偏移量+限制+1)、总计、限制);
}
否则{
this.auditTrailHistory=this.auditTrailHistory.concat(response.data.data);
}
})
.catch(error=>console.error(error));
}
我认为你缺少了一些,你中的返回了
async parseAuditTrailHistoryPage(audittrail, startDate, endDate, offset, total, limit) {
console.log(new Date() + " - " + startDate + " - " + endDate + " - " + offset + " - " + total + " - " + limit);
return this.axiosInstance
.get('/api/v20.2/audittrail/' + audittrail + '?start_date=' + startDate + '&end_date=' + endDate + '&limit=' + limit + '&offset=' + offset, this.config)
.then(response => {
if ((offset+limit) < total) {
this.auditTrailHistory = this.auditTrailHistory.concat(response.data.data);
// return promise (add it to the end of the promise chaine)
return this.parseAuditTrailHistoryPage(audittrail, startDate, endDate, (offset+limit+1), total, limit);
}
else {
this.auditTrailHistory = this.auditTrailHistory.concat(response.data.data);
}
})
.catch(error => console.error(error));
}
async parseAuditTrailHistoryPage(audittrail、startDate、endDate、offset、total、limit){
日志(新日期()+“-”+开始日期+“-”+结束日期+“-”+偏移量+“-”+总计+“-”+限制);
返回此参数。axiosInstance
.get('/api/v20.2/audittrail/'+audittrail+'?开始日期='+startDate+'&结束日期='+endDate+'&限制='+limit+'&偏移='+offset,this.config)
。然后(响应=>{
如果((偏移+限制)<总计){
this.auditTrailHistory=this.auditTrailHistory.concat(response.data.data);
//返回承诺(将其添加到承诺链的末尾)
返回此.parseAuditTrailHistoryPage(audittrail、startDate、endDate,(偏移量+限制+1)、总计、限制);
}
否则{
this.auditTrailHistory=this.auditTrailHistory.concat(response.data.data);
}
})
.catch(error=>console.error(error));
}
如果没有修复,则parseAuditTrailHistoryPage
将不会等待内部parseAuditTrailHistoryPage
解析。有了这个修正,递归的每次迭代都将等待下一次直到最后一次(当(offset+limit)
同样的问题出现在collectUserName
,collectAuditTrailHistory
,Hi@Daniel,非常感谢您的及时回复。你说得对,我想
Promise.all([
vault.collectAuditTrailHistory('document_audit_trail', startDate, endDate),
vault.collectGroupMembers(6)
]).then(result => {
console.log("test");
})
async login() {
return this.axiosInstance
.post('/api/v20.2/auth', 'username=' + this.username + '&password=' + this.password, this.config)
.then(response => {
if (response.data.responseStatus = "SUCCESS") {
this.axiosInstance.defaults.headers.common['Authorization'] = response.data.sessionId;
}
})
.catch(error => console.error(error));
}
async collectAuditTrailHistory(audittrail, startDate, endDate) {
// check valid login
if (this.isLoggedIn()) {
return this.axiosInstance
.get('/api/v20.2/audittrail/'+ audittrail +'?start_date=' + startDate + '&end_date=' + endDate + '&limit=1', this.config)
.then(response => {
this.parseAuditTrailHistoryPage(audittrail, startDate, endDate, 0, response.data.responseDetails.total, 1000);
})
.catch(error => console.error(error));
} else {
return this.login()
.then((response) => {
this.collectAuditTrailHistory(audittrail, startDate, endDate);
});
}
}
async collectGroupMembers(groudId) {
// check valid login
if (this.isLoggedIn()) {
return this.axiosInstance
.get('/api/v20.2/objects/groups/' + groudId, this.config)
.then(response => {
for(let i = 0; i < response.data.groups[0].group.members__v.length; i++){
this.collectUserName(groudId, response.data.groups[0].group.members__v[i]);
}
})
.catch(error => console.error(error));
} else {
return this.login()
.then((response) => {
return this.collectGroupMembers(groudId);
});
}
}
async collectUserName(groudId, userId) {
// check valid login
if (this.isLoggedIn()) {
return this.axiosInstance
.get('/api/v20.2/objects/users/' + userId, this.config)
.then(response => {
console.log(new Date() + " - " + groudId + " - " + userId);
this.groupMembers.push([groudId,response.data.users[0].user.user_name__v]);
})
.catch(error => console.error(error));
} else {
return this.login()
.then((response) => {
return this.collectUserName(groudId, userId);
});
}
}
isLoggedIn() {
if (!this.axiosInstance.defaults.headers.common['Authorization'] || !this.lastTxnDateTime)
return false;
else {
let diff = Math.abs(new Date() - this.lastTxnDateTime);
if (Math.floor((diff/1000)/60) < 15) {
return true;
}
else {
return false;
}
}
}
async parseAuditTrailHistoryPage(audittrail, startDate, endDate, offset, total, limit) {
console.log(new Date() + " - " + startDate + " - " + endDate + " - " + offset + " - " + total + " - " + limit);
return this.axiosInstance
.get('/api/v20.2/audittrail/' + audittrail + '?start_date=' + startDate + '&end_date=' + endDate + '&limit=' + limit + '&offset=' + offset, this.config)
.then(response => {
if ((offset+limit) < total) {
this.auditTrailHistory = this.auditTrailHistory.concat(response.data.data);
this.parseAuditTrailHistoryPage(audittrail, startDate, endDate, (offset+limit+1), total, limit);
}
else {
this.auditTrailHistory = this.auditTrailHistory.concat(response.data.data);
}
})
.catch(error => console.error(error));
}
async parseAuditTrailHistoryPage(audittrail, startDate, endDate, offset, total, limit) {
console.log(new Date() + " - " + startDate + " - " + endDate + " - " + offset + " - " + total + " - " + limit);
return this.axiosInstance
.get('/api/v20.2/audittrail/' + audittrail + '?start_date=' + startDate + '&end_date=' + endDate + '&limit=' + limit + '&offset=' + offset, this.config)
.then(response => {
if ((offset+limit) < total) {
this.auditTrailHistory = this.auditTrailHistory.concat(response.data.data);
// return promise (add it to the end of the promise chaine)
return this.parseAuditTrailHistoryPage(audittrail, startDate, endDate, (offset+limit+1), total, limit);
}
else {
this.auditTrailHistory = this.auditTrailHistory.concat(response.data.data);
}
})
.catch(error => console.error(error));
}