需求:两个异步函数按顺序执行,首先获取第一个异步函数的返回的值,接着在第二个异步函数里面调用

方法:先在第一个异步函数里返回一个promise,接着用async和await调用它

第一个异步方法:

getAllNotice() {
				let data = {
					"searchParams": [{
						"fieldName": "equipmentId",
						"operate": "eq",
						"value": "000000"
					}],
					"size": -1
				}
				return new Promise((resolve) => {
					API.getNotice(data).then(res => {
						console.log(res)
						if (res.data.code == "200") {
							this.noticeList = res.data.data.list
							console.log(this.noticeList)
							resolve();
							return
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 1000,
								icon: "none"
							})
						}
					})
				})
				
			},

第二个异步方法:

//获得当前的公告列表
			getNowNotice(){
				//获取当前时间戳
				var timestamp = (new Date()).getTime();
				var _this = this
				console.log(timestamp);
				//将noticeList的结束时间转换成时间戳
				for(var i=0; i<this.noticeList.length; i++){
					var endTimeStamp = TIME.TimeToTimeStamp(this.noticeList[i].endTime)
					console.log(endTimeStamp)
					if(endTimeStamp>timestamp){
						_this.noticeNewList.push(this.noticeList[i])
					}
				}
				console.log("noticeNewList",_this.noticeNewList)
			}

用async和await

async onLoad(option) {
			await this.getAllNotice()
			await this.getNowNotice()
		},
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐