Promise.all 在vue写法

75 阅读1分钟
let {promises, callbacks} = this.requestList();
Promise.all(promises.map(p => p.catch(e => ({promiseStatus: 'fullfailed', error: e}))))
    .then(res => {
        console.log(res,"res")
        res.forEach((item, index) => {
            if (item.promiseStatus !== 'fullfailed') {
                let callback = callbacks[index];
                if (callback && typeof callback === 'function') callback.call(this, item);
            } else {
                console.error(item.error);
            }
            this.loading = false;
        });
    })
    .catch(e => {
        console.error(e);
        this.loading = false;
    });
requestList() {
    const requestObj = [
        {
            promise: getHot({...this.searchData, maxResult: 10}),
            callback(res) {
                this.hotList = res || [];
                this.loading8 = false;
            }
        },
        {
            promise: getMap(this.searchData),
            callback(res) {
                this.mapOption = mapFn(res || [], this.org.name);
            }
        },
        {
            promise: getTrends(this.searchData),
            callback(res) {
                this.pieOption = trendFn(res || []);
                this.loading3 = false;
            }
        },
        {
            promise: getTrend(this.searchData),
            callback(res) {
                this.loading1 = false;
                this.lineOption = lineFn(res);
                this.selectallList = this.lineOption.legend ? this.lineOption.legend.data : [];
            }
        },
        
    ];
    let promises = [];
    let callbacks = [];
    requestObj.map(item => {
        promises.push(item.promise);
        callbacks.push(item.callback);
    });
    return {
        promises,
        callbacks
    };
},