美文网首页
Promise结合reduce构建顺序执行队列

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

作者: AR7_ | 来源:发表于2020-07-07 15:28 被阅读0次
            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构建顺序执行队列

    相关文章

      网友评论

          本文标题:Promise结合reduce构建顺序执行队列

          本文链接:https://www.haomeiwen.com/subject/rsoaqktx.html