簡體   English   中英

Promise.all 與 axios / node.js

[英]Promise.all with axios / node.js

我在這里試圖等待來自兩個或多個 axios 承諾的結果以進一步處理它們,這有點卡住了。 控制台日志(“測試”); 在其他方法調用完全執行之前正在執行。 我覺得我如何設置遞歸解析不正確但不確定要調整什么。 這里有人有想法嗎? 非常感謝。

輸出 - -

test
Mon Sep 14 2020 14:04:37 GMT+0100 (Irish Standard Time) - 2020-09-13T00:00:00Z - 2020-09-14T00:00:00Z - 0 - 14897 - 1000
Mon Sep 14 2020 14:04:37 GMT+0100 (Irish Standard Time) - 6 - 3247395

指數 - -

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));
    }

我想你then缺少一些return

    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));
    }

如果沒有修復, parseAuditTrailHistoryPage將不會等待內部parseAuditTrailHistoryPage解析。 通過修復,遞歸的每次迭代都會等待下一次直到最后一次(when (offset+limit) < total == false)

collectUserNamecollectAuditTrailHistory相同問題,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM