当前位置:网站首页>Small program npm package--API Promise
Small program npm package--API Promise
2022-07-30 04:45:00 【like Feynman %】
1. Disadvantages of callback-based asynchronous API
By default, the asynchronous APIs officially provided by Mini Programs are implemented based on callback functions. For example, the API for network requests needs to be called as follows:
wx.request({method:'',url:'',data:{},success:()=>{},//Callback function for successful requestfail:()=>{},//Callback function for request failurecomplete:()=>{}//Callback function for request completion})Disadvantages: It is easy to cause the problem of callback hell, and the readability and maintainability of the code are poor!
2. What is API Promise
API Promise refers to upgrading the officially provided asynchronous API based on callback functions, to Promise-based through additional configurationThe asynchronous API, from 3. to improve the readability, maintainability of the code, and avoid the problem of callback hell
3. Realize Promise
In the miniprogram, the realization of API Promise mainly depends on the third-party npm package miniprogram-api-promise.
npm i --save [email protected]
//app.jsimport{ promisifyAll } from 'miniprogram-api-promise'const wxp = wx.p={}promisifyAll(wx,wxp)4. Call the asynchronous API after Promiseization
//page.jsasync getInfo(){const{data: res }=await wx.p.request({method:'GET',url:'https://www.escook.cn/api/get',data:{name:'zs',age:20}})console.log(res)},//page.wxmlButton 
For the non-promise WeChat callback function, please refer to the article: http://t.csdn.cn/yPCxV
边栏推荐
- Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
- 2.5快速排序
- Weight line segment tree + line segment tree split/merge + CF1659D
- "Translation" Envoy Fundamentals, this is a training course, make people to more quickly using Envoy Proxy..
- 2.6 Merge Sort
- My first experience of Go+ language——Blessing message system, so that she can also feel your blessings
- Discourse Custom Header Links
- See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
- Install MySQL Database on Kylin V10 Operating System
- MySQL String Concatenation - Various String Concatenation Practical Cases
猜你喜欢

Learning of redis_Basic part

Stimulsoft ReportsJS and DashboardsJS. 2022.3.3

DAY17, CSRF vulnerability

Chapter8 支持向量机

小程序npm包--API Promise化

Six, read application configuration + log configuration

Protobuf compound data types, speaking, reading and writing

labelme的使用技巧

Unity beginner 5 cameras follow, border control and simple particle control (2 d)

1. 获取数据-requests.get()
随机推荐
My first experience of Go+ language——Blessing message system, so that she can also feel your blessings
聊一聊什么是SaaS,以及遇到的问题......
Classification of decision tree classification
2.4希尔排序
LeetCode Algorithm 2326. 螺旋矩阵 IV
Double pointer problem (middle)
The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
模拟问题(上)
小程序npm包--API Promise化
Requirements design document and the changing role of the product manager
[MRCTF2020]Hello_misc
模拟问题(中)
Naive Bayes Classification
KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
VUX Datetime 组件compute-days-function动态设置日期列表
【Redis高手修炼之路】Jedis——Jedis的基本使用
KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
file system two
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
(Problem practice) Conditional probability + weight line segment tree + FWT + suffix array