微信小程序 引入es6 promise
微信小程序開發(fā)兩個月了.大家的項目都在不斷迭代.已經(jīng)不是小程序.這時候就會遇到多層回調(diào)嵌套的問題.有些目不忍視了.迫不得已引入es6-promise.在微信小程序內(nèi)測的時候promise不需要手動引入,后來被微信移除了.看看效果.
promise詳細(xì)的介紹我就不說了.有很多大神寫過.
看看目錄,引入es6-promise就可以用了.
目錄
1.網(wǎng)絡(luò)請求 wxRequest.js
這里只寫了get和post.
我經(jīng)常會在網(wǎng)絡(luò)請求的時候用微信原生showToast(),所以最后加了finally,方便hideToast()
var Promise = require('../plugins/es6-promise.js') function wxPromisify(fn) { return function (obj = {}) { return new Promise((resolve, reject) => { obj.success = function (res) { //成功 resolve(res) } obj.fail = function (res) { //失敗 reject(res) } fn(obj) }) } } //無論promise對象最后狀態(tài)如何都會執(zhí)行 Promise.prototype.finally = function (callback) { let P = this.constructor; return this.then( value => P.resolve(callback()).then(() => value), reason => P.resolve(callback()).then(() => { throw reason }) ); }; /** * 微信請求get方法 * url * data 以對象的格式傳入 */ function getRequest(url, data) { var getRequest = wxPromisify(wx.request) return getRequest({ url: url, method: 'GET', data: data, header: { 'Content-Type': 'application/json' } }) } /** * 微信請求post方法封裝 * url * data 以對象的格式傳入 */ function postRequest(url, data) { var postRequest = wxPromisify(wx.request) return postRequest({ url: url, method: 'POST', data: data, header: { "content-type": "application/x-www-form-urlencoded" }, }) } module.exports = { postRequest: postRequest, getRequest: getRequest }
2.微信其他API wxApi.js
var Promise = require('../plugins/es6-promise.js') function wxPromisify(fn) { return function (obj = {}) { return new Promise((resolve, reject) => { obj.success = function (res) { //成功 resolve(res) } obj.fail = function (res) { //失敗 reject(res) } fn(obj) }) } } //無論promise對象最后狀態(tài)如何都會執(zhí)行 Promise.prototype.finally = function (callback) { let P = this.constructor; return this.then( value => P.resolve(callback()).then(() => value), reason => P.resolve(callback()).then(() => { throw reason }) ); }; /** * 微信用戶登錄,獲取code */ function wxLogin() { return wxPromisify(wx.login) } /** * 獲取微信用戶信息 * 注意:須在登錄之后調(diào)用 */ function wxGetUserInfo() { return wxPromisify(wx.getUserInfo) } /** * 獲取系統(tǒng)信息 */ function wxGetSystemInfo() { return wxPromisify(wx.getSystemInfo) } module.exports = { wxPromisify: wxPromisify, wxLogin: wxLogin, wxGetUserInfo: wxGetUserInfo, wxGetSystemInfo: wxGetSystemInfo }
3.用法
promise應(yīng)用場景很多,下面是promise最基本的用法,在then()中returnpromise對象.
這樣有效解決了回調(diào)嵌套的問題.讓代碼看起來更優(yōu)雅.可讀性更高.
var util = require('../../utils/util') var wxApi = require('../../utils/wxApi') var wxRequest = require('../../utils/wxRequest') import config from '../../utils/config' //獲取應(yīng)用實例 var app = getApp() Page({ data: { userInfo: {} }, onLoad: function () { var that = this; wx.showToast({ title: '加載中', icon: 'loading', duration: 10000 }) //1.獲取code var wxLogin = wxApi.wxLogin() wxLogin().then(res => { console.log('1.成功了') console.log(res.code) var url = config.getOpenidUrl; var params = { appid: "wxed7******2d465", secret: "e9c5e4c******09ecc5ebd811", js_code: res.code, grant_type: "authorization_code" } //2.獲取openid return wxRequest.getRequest(url, params) }). then(res => { console.log('2.成功了') console.log(res) var url = app.globalData.ip + config.searchDgUrl var data = util.json2Form({ phoneNumber: '15971908021' }) //3.獲取綁定手機號碼 return wxRequest.postRequest(url, data) }). then(res => { console.log('3.成功了') console.log(res) //4.獲取系統(tǒng)信息 var wxGetSystemInfo = wxApi.wxGetSystemInfo() return wxGetSystemInfo() }). then(res => { console.log('4.成功了') console.log(res) //5.獲取用戶信息 var wxGetUserInfo = wxApi.wxGetUserInfo() return wxGetUserInfo() }). then(res => { console.log('5.成功了') console.log(res.userInfo) that.setData({ userInfo: res.userInfo }) }) .finally(function (res) { console.log('finally~') wx.hideToast() }) } })
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
layui table 列寬百分比顯示的實現(xiàn)方法
今天小編就為大家分享一篇layui table 列寬百分比顯示的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript實現(xiàn)網(wǎng)頁跨年倒計時
這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)網(wǎng)頁跨年倒計時,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12