Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use es6-promise.js to encapsulate requests and handle asynchronous processes in WeChat Mini Programs

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要介绍"微信小程序中怎么使用es6-promise.js封装请求与处理异步进程",在日常操作中,相信很多人在微信小程序中怎么使用es6-promise.js封装请求与处理异步进程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"微信小程序中怎么使用es6-promise.js封装请求与处理异步进程"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

微信小程序 es6-promise.js封装请求与处理异步进程

下载es6-promise.js置于根目录下的libs文件夹下;

在根目录utils文件夹下新建httpsPromisify.js,即定义封装请求的方法

var Promise = require('../libs/es6-promise.min')function httpsPromisify(fn) { return function (obj = {}) { return new Promise((resolve, reject) => { obj.success = function (res) { resolve(res) } obj.fail = function (res) { reject(res) } fn(obj) }) }}module.exports = { httpsPromisify: httpsPromisify}

调用方法:

var Promisify = require('../../utils/httpsPromisify') Page({ onLoad: function(){ Promisify.httpsPromisify(wx.request)({ url: "https://XXXXXXX", header: { "Content-Type": "application/x-www-form-urlencoded" }, method: "POST", data: { } }).then(function(res){ console.log(res) }) }, })

注意:

目前支持promise的第三方库有许多,如$q.js,bluebird.js等等,但是需要注意的是,这些在微信开发工具上可以正常使用,但是到真机上就没有效果了;

所以用es6-Promise.js,这个亲测完美,而且文件大小比其他在都小很多,所以建议大家使用。

到此,关于"微信小程序中怎么使用es6-promise.js封装请求与处理异步进程"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report