当前位置:网站首页>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
边栏推荐
- How does MySql find out the latest data row that meets the conditions?
- The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Part of the WP (9)
- Seven, custom configuration
- Chapter8 Support Vector Machines
- Alibaba Cloud's EasyNLP Chinese text image generation model takes you to become an artist in seconds
- Simple experiment with BGP
- 2.6基数排序(桶排序)
- protobuf 中复合数据类型的读写
- 小程序 wx.miniProgram.navigateTo 跳转地址不能是tabbar地址
- Image stitching (registration) case based on OpenCV
猜你喜欢

Simple experiment with BGP

三、依赖配置管理

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

Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference

五、视图解析与模板引擎

Code readability, pre-checks, comments and summaries

@ WebServlet annotations (Servlet annotations)

The Complete Go Books - Beginner to Advanced and Web Development

【线性表】- LeetCode力扣三道练习题详解

1. 获取数据-requests.get()
随机推荐
Catch That Cow(详解)
1. Get data - requests.get()
The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
【线性表】- LeetCode力扣三道练习题详解
Verify that the addShutdownHook hook takes effect
山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
LeetCode Algorithm 328. 奇偶链表
My first experience of Go+ language——Blessing message system, so that she can also feel your blessings
The Double Pointer Problem (Part 1)
Web page element parsing a tag
LeetCode Algorithm 2326. 螺旋矩阵 IV
Get the local IP and Request's IP
Chapter8 Support Vector Machines
Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b
小程序npm包--API Promise化
WPF introduces ttf icon file usage record
模拟问题(中)
Simulation Problem (Part 1)
Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference
05 Detailed explanation of the global configuration file application.properties