uni-app内 async 和 await 的使用方法

6 篇文章 1 订阅
5 篇文章 0 订阅

async await 最常见的是在云函数内的使用

  • await 只能用于 async 函数内部
  • await 使用的精髓在于,紧随 await 之后的函数或是其他类型,产生的结果,一旦被调用就会立即计算出结果,供调用者使用。
`resData 一旦被使用,即使只是打印了一下,也会优先于 for 循环,先行处理`
exports.main = async (event, context) => {
	let resData = await db.collection('demo').field({_id:0}).get();	
	console.log("是我调用await结果的,我先",resData);
	for(let i =0;i<10;i++){
		console.log('我想先执行可以吗?',i)
		}
	return{
		code:200,
		msg:'请求成功',
		resData
	}
}	

async await 在 methods 内使用

  • 在一个方法的内部使用 async await
  • 这个方法在等待另一个方法的结果
methods: {
  'myRequest()函数内部,需要等待网络请求出结果了'
  '再把数据返回给其他函数调用处'
  async myRequest(val) {
	const [Error, res] = await uni.request({
		url: '/autonumber/autoComNum',
		method: 'GET',
		data: {
			text: val
			}
		});
		let data = res.data;
		let err = Error;
		return { data, err };
	},
	'检查验证数据的checkNumB()函数内部'
	'需要等待三个函数的结果,才能进行下一步'
  async checkNumB() {
	/*检测快递单号的长度*/
	let checkLength = await this.checkLength(this.numB);
	if (!checkLength) {
		this.remove();
		console.log("单号长度有误")
		return;
	} /*检测快递单号是否重复*/
	let myforArr = await this.forArr(this.numB);
	if (!myforArr) {
		this.remove();
		console.log('单号重复');
		return;		
	}
	/*网络检测快递单号是否正确*/
	let { data, err } = await this.myRequest(this.numB);
	if (data) {
		this.expressName = data;
		this.isRequest = true;
		} else {
			console.log(err);
			this.wrong += 1;
			this.isRequest = false;
			this.expressName = '-未知-';
		}
		this.totalCount();
	},
  'forArr()是一个普通函数'
  forArr(e) {
	var arr = this.buffNumArr.filter(k => {
		return k === e;
	});
	if (arr.length !== 0) {
		this.isFalse = true;
		this.num += 1;
		this.wrong;
		return false;
	} else {
		return true;
	}
  },
  'checkLength()是一个普通函数'
  checkLength(e) {
	if (e.length < 12) {
		this.wrong += 1;
		this.countdown = `快递单号长度有误${this.wrong}次`;
		this.istrue = true;
		return false;
	    } else {
		  return true;
	   }
   }
}		
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
uni-app使用 async/await 主要有以下几个步骤: 1. 将需要异步处理的函数改为 async 函数,并在其中使用 await 关键字等待异步操作完成。 2. 在调用 async 函数的地方使用 try/catch 块处理可能出现的异常。 下面是一个使用 async/await 的例子: ```javascript // 定义一个异步函数 async function getData() { const res = await uni.request({ url: 'https://some-api.com/data', method: 'GET' }); if (res[0]) { return res[1]; } else { throw new Error('请求失败'); } } // 在调用 getData 的地方使用 try/catch 块处理可能出现的异常 try { const data = await getData(); console.log(data); } catch (err) { console.error(err); } ``` 在上面的代码中,`getData` 函数使用了 `await` 等待 `uni.request` 异步操作完成。在使用 `getData` 函数时,我们使用了 try/catch 块来处理可能出现的异常。 需要注意的是,`async` 函数返回的是一个 `Promise` 对象,因此,在使用 `async` 函数时,我们可以将其返回值赋值给一个变量,并使用 `then` 方法获取异步操作的结果,如下所示: ```javascript async function getData() { const res = await uni.request({ url: 'https://some-api.com/data', method: 'GET' }); if (res[0]) { return res[1]; } else { throw new Error('请求失败'); } } getData().then(data => { console.log(data); }).catch(err => { console.error(err); }); ``` 上面的代码中,我们使用 `then` 方法获取 `getData` 函数的异步结果,并使用 `catch` 方法处理异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值