循环同步请求同一个接口

原来的: 

             async getData() {
				let that = this;
				//重设日期标题
                this.doctorStatus = [];
				this.headList = [];
				this.dataList = [];
				for (let i = 0; i < this.winSteps; i++) {
					this.doctorStatus[i] = [];
					this.dataList[i] = [];
				}
				for (let i = 0; i < this.winSteps; i++) {
					let title = "";
					title = (() => {
						if (i == 0) {
							return "今天"
						}
						if (i == 1) {
							return "明天"
						}
						if (i == 2) {
							return "后天"
						}
						return this.$util.dateFormat(this.$util.getDatePoint(this.date_start, 0, 0, i ), "周w")
					})();
					this.headList.push({
						title: title,
						dateStr: this.$util.dateFormat(this.$util.getDatePoint(this.date_start, 0, 0, i ),
							"MM-DD")
					})
				}

                // 预约号源
                let questDoctor_future = {
                	"serviceCode": "getDoctorSchedulingList",
                    "doctorCode":this.doctorCode,
                	"deptCode": this.deptCode,
					"beginDate": this.date_start,
					"endDate": this.date_end,
                }
				await this.$http.post(questDoctor_future, (res) => {
					if(res.data.resultCode == 0){
				        for (var item of res.data.item) {
                            var arr = [item.morningNum,item.afternoonNum,item.eveningNum];
				        	var addIdx = that.$util.getDateDiffer(item.scheduleDate, that.date_start) ;
				        	that.doctorStatus[addIdx].push(item);
                            if(item.morningWorkStatu == -1){
                                arr[0] = NaN
                            }
                            if(item.afternoonWorkStatu == -1){
                                arr[1] = NaN
                            }
                            if(item.noonWorkStatu == -1){
                                arr[2] = NaN
                            }
                             for(var i=0;i<=arr.length;i++){
                                 if(arr[i] && arr[i] > 0){
                                     //挂号号源数据
                                     let queryParams = {
                                         "serviceCode": "getDoctorRegResource",
                                         "scheduleDate": item.scheduleDate,
                                         "timeFlag": i+1,
                                         "deptCode": that.deptCode,
                                         "doctorCode": that.doctorCode,
                                     }
									 that.getDoctorRegResource(queryParams);
                                 }
                             }
				        }
				       
						
					}
				}, {noMsg:true});
			},
			async getDoctorRegResource(queryParams) {
				let that = this;
				await this.$http.post(queryParams, (res) => {
					that.done.future = true;
					if (res.data.resultCode == 0) {
						for (let item of res.data.item) {
							let addIdx = that.$util.getDateDiffer(item.scheduleDate, this.date_start) ;
							that.dataList[addIdx].push(item);
						}
						that.$forceUpdate();
					}
				}, {noMsg:true})
			},

 循环同步请求同一个接口_第1张图片

逻辑错了 要做的是一个接口的延时 而不是不同接口 

例子:

let  outer = ['out1','out2','out3'],inner = ['in1','in2','in3'];

async function runTask(){
    const timeout = (inner)=>{
        return new Promise(res=>{
            setTimeout(()=>{
                res()
            },2000)
        })
    }
    console.log('任务开始')
    for (let index = 0; index < outer.length; index++) {
        const o = outer[index];
        for (let index = 0; index < inner.length; index++) {
            const i = inner[index];
            await timeout(i)
            console.log(`inner里的${i}完成`)
        }
    }}
    console.log('任务完成')

runTask()

 修改后:


			 getData() {
				let that = this;
				//重设日期标题
                this.doctorStatus = [];
				this.headList = [];
				this.dataList = [];
				for (let i = 0; i < this.winSteps; i++) {
					this.doctorStatus[i] = [];
					this.dataList[i] = [];
				}
				for (let i = 0; i < this.winSteps; i++) {
					let title = "";
					title = (() => {
						if (i == 0) {
							return "今天"
						}
						if (i == 1) {
							return "明天"
						}
						if (i == 2) {
							return "后天"
						}
						return this.$util.dateFormat(this.$util.getDatePoint(this.date_start, 0, 0, i ), "周w")
					})();
					this.headList.push({
						title: title,
						dateStr: this.$util.dateFormat(this.$util.getDatePoint(this.date_start, 0, 0, i ),
							"MM-DD")
					})
				}

                let questDoctor_future = {
                	"serviceCode": "getDoctorSchedulingList",
                    "doctorCode":this.doctorCode,
                	"deptCode": this.deptCode,
					"beginDate": this.date_start,
					"endDate": this.date_end,
                }
				 this.$http.post(questDoctor_future, (res) => {
					if(res.data.resultCode == 0){
						that.doctorSchedulingList = res.data.item
                        //调用
						that.getDoctorRegResource()
					}
				}, {noMsg:true});
			},
			async getDoctorRegResource() {
				let that = this;
                /****** 关键 *******/
				const timeout = (queryParams,item)=>{
					return new Promise(re=>{
                        //请求
						this.$http.post(queryParams, (res) => {
							that.done.future = true;
							if (res.data.resultCode == 0) {
								for (let item of res.data.item) {
									let addIdx = that.$util.getDateDiffer(item.scheduleDate, this.date_start) ;
									that.dataList[addIdx].push(item);
								}
								re()
								that.$forceUpdate();
							}
						}, {noMsg:true})
					})
				}

				for (var item of this.doctorSchedulingList) {
					var arr = [item.morningNum,item.afternoonNum,item.eveningNum];
					var addIdx = that.$util.getDateDiffer(item.scheduleDate, that.date_start) ;
					that.doctorStatus[addIdx].push(item);
					if(item.morningWorkStatu == -1){
						arr[0] = NaN
					}
					if(item.afternoonWorkStatu == -1){
						arr[1] = NaN
					}
					if(item.noonWorkStatu == -1){
						arr[2] = NaN
					}
					for(var i=0;i<=arr.length;i++){
						if(arr[i] && arr[i] > 0){
							let queryParams = {
								"serviceCode": "getDoctorRegResource",
								"scheduleDate": item.scheduleDate,
								"timeFlag": i+1,
								"deptCode": that.deptCode,
								"doctorCode": that.doctorCode,
							}
                            //调用
							await timeout(queryParams,item);
						}
					}
				}
			},

循环同步请求同一个接口_第2张图片

你可能感兴趣的:(js,前端,javascript)