promise设置多个接口并行调用

promise如何设置多个接口并行调用呢?这里我使用了promise all的方式,一开始让三个接口同时调用,代码如下图所示。

    mounted: function () {
        this.$nextTick(function () {
            this.fetchAll();
        })
    },
   fetchAll() {
            let self = this;
            // let ajaxUrl = 'http://10.253.33.109:80';
            let detailsOfEvent = new Promise((resolve, reject) => {
                http.fetchPost(`${ajaxUrl}/api/v1/ui/match/team`, {
                        "gameAbbr": "DOTA2",
                        "platform": "admin",
                        "cupId": 0,
                        "teamId": parseURL(location.href).params.team_id
                    })
                    .then(function (res) {
                        resolve(res);
                    })
            })

            let monthlyAchievements = new Promise((resolve, reject) => {
                http.fetchPost(`${ajaxUrl}/api/v1/ui/match/teamMonthRecord`, {
                        "gameAbbr": "DOTA2",
                        "platform": "admin",
                        "teamId": parseURL(location.href).params.team_id
                    })
                    .then(function (res) {
                        resolve(res)
                    })
            })

            let recentCompetitions = new Promise((resolve, reject) => {
                http.fetchPost(`${ajaxUrl}/api/v1/ui/match/teamRecentMatches`, {
                        "gameAbbr": "DOTA2",
                        "platform": "admin",
                        "teamId": parseURL(location.href).params.team_id
                    })
                    .then(function (res) {
                        resolve(res)
                    })
            })

            Promise.all([detailsOfEvent, monthlyAchievements, recentCompetitions]).then((result) => {
                self.setShowAll(result);
                self.isShowLoading = false;
            }, () => {
                self.isShowLoading = false;
                self.portError = true;
            }).catch((error) => {
                self.isShowLoading = false;
                //console.log(error)
            })
        },

猜你喜欢

转载自blog.csdn.net/qq_24147051/article/details/92791998
今日推荐