关于Promise的知识和async及其await的使用

本文介绍了如何在JavaScript中使用Promise进行网络请求,并通过async/await简化异步操作。通过实例展示了uni.request的使用和async函数处理获取微信信息和手机号码的过程。
摘要由CSDN通过智能技术生成

关于Promise的知识和async及其await的使用

new Promise(function(resove, reject){
							uni.request({
								url: getApp().globalData.Interfaceurl + 'grantWxInfo',
								method: "post",
								header: {
									"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
								},
								data: {
									userId: getApp().globalData.userId,
									rawData: res.rawData,
									signature: res.signature,
									encryptedData: res.encryptedData,
									iv: res.iv
								},
								success(rest) {
									resove(rest)
									// console.log(rest)
								},
								fail(errs) {
									// reject(errs)
							
								}
							})
						}).then(
						res=>console.log(res,2222),
						err=>console.log(err,3333)
						)
async和await的使用
async getPhoneNumber(e) {
				// console.log(e)
				await new Promise(function(resove,reject){
					uni.request({
						url: getApp().globalData.Interfaceurl + 'grantWxMobile',
						method: "post",
						header: {
							"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
						},
						data: {
							userId: getApp().globalData.userId,
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
						},
						success(res) {
							resove(res)
							 console.log(res)
						},
						error(err){
							console.log(err)
							
						}
					})
				})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值