Promise.all方法

131 阅读1分钟

Promise的all方法提供了并行执行异步操作的能力,并且在所有异步操作执行完后才执行回调。可以将多个 Promise 实例

下面是我的小程序一段代码。

1.在methods里写了三个异步函数

2.请求的三个数据保存在data里

3.onPullDownRefresh是小程序的下拉刷新方法 (这个方法需要配置才可以使用)

3.1.清空data数据

3.2.Promise.all([])方法数组里写入我们的三个请求函数

3.3.await 完成后 执行uni.stopPullDownRefresh() 关闭小程序下拉状态

` data() {

		return {
			banelist: [],
			navlist: [],
			mianlist:[]
		}
	},
            
	async onPullDownRefresh(){
			this.banelist= [],
			this.navlist= [],
			this.mianlist=[]
			// Promise.all(【】)  数组内全部请求完成后处理
        await Promise.all([
			this.getbane(),
            this.getnav(),
            this.getmian()
		])
		uni.stopPullDownRefresh()    
	},
	onLoad() {
		this.getbane()
		this.getnav()
		this.getmian()
	},
	methods: {
		async getbane() {
			const [err, res] = await uni.request({
				url: 'https://',
				methods: 'get'
			})
			if (err) {
				console.log(err);
			} else {
				this.banelist = res.data.message
			}
		},
		async getnav() {
			const [err, res] = await uni.request({
				url: 'https://',
				methods: 'get'
			})
			if (err) {
				console.log(err);
			} else {
				this.navlist = res.data.message
			}
		},
		async getmian(){
			const [err, res] = await uni.request({
				url: 'https://',
				methods: 'get'
			})
			if(err){
				console.log(ree);
			}else{
				this.mianlist=res.data.message
			}
		}
	},

`