In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 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置于根目录下的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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.