Promise结合reduce构建顺序执行队列

2020-07-07  本文已影响0人  AR7_
        updateEventStatus(paramsList) {
            const createPromise = params => () => {
                return new Promise((resolve, reject) => {
                    XXXXXApi.XXXXXDeviceEvent.updateXXXDeviceEventById(params).then(res => {
                        for (let i = 0; i < this.processParams.selectedList.length; i++) {
                            if (params.eventid === this.processParams.selectedList[i].eventid) {
                                this.processParams.selectedList[i].status = { text: '成功', color: '#67c23a' }
                            }
                        }
                        resolve(res)
                    }).catch(error => {
                        for (let i = 0; i < this.processParams.selectedList.length; i++) {
                            if (params.eventid === this.processParams.selectedList[i].eventid) {
                                this.processParams.selectedList[i].status = { text: '失败', color: '#f56c6c' }
                            }
                        }
                        reject(error)
                    })
                })
            }
            let tasks = []
            paramsList.forEach(item => {
                tasks.push(createPromise(item.params))
            })
            let doTask = tasks.reduce((prev, next) => prev.then(res => next()), Promise.resolve())
            doTask.then(res => {
                this.$emit('confirm')
            }).catch(error => {
                console.log(error)
            })
        },

Promise结合reduce构建顺序执行队列

上一篇下一篇

猜你喜欢

热点阅读