async与await的使用 --- vue

异步接口调用:

  1. 不返回值:
	async queryData(){
		try{
			await Promise.all([
			this.func1(),
			this.func2()
		])
		}catch(e){}
	}
	
	//方法一
	async func1(){
		let param={}
		let res=this.$ajax.psot(api,param)
		//res  接口值
	}

	//方法二
	async func2(){
		let param = {};
			try {
				let res = await this.$ajax.post(api, param);
				//res 接口数据
			}
			catch (err) {
				console.error(err.msg);
			}
	}

  1. 返回值
	async queryData(){
		try{
			let [data1,data2]=await Promise.all([
				this.func1(),
				this.func2()
			])
			//data1-func1返回数据
			//data2-func2返回阿虎局
		}.catch()
	}
	
	//方法一
	async func1(){
		let param={}
		return await this.$ajax.post(api,parma).then(res=>{
			return res;
		}) 
		}
	}
	
	//方法二
	async func2(){
		let param={}
		return await this.$ajax,post(api,param).then(res=>{
			return res;
		})
	}




猜你喜欢

转载自blog.csdn.net/weixin_38961329/article/details/99635764