微信小程序開發(fā)兩個(gè)月了.大家的項(xiàng)目都在不斷迭代.已經(jīng)不是小程序.這時(shí)候就會(huì)遇到多層回調(diào)嵌套的問(wèn)題.有些目不忍視了.迫不得已引入es6-promise.在微信小程序內(nèi)測(cè)的時(shí)候promise不需要手動(dòng)引入,后來(lái)被微信移除了.看看效果.
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、昌邑網(wǎng)站維護(hù)、網(wǎng)站推廣。
promise詳細(xì)的介紹我就不說(shuō)了.有很多大神寫過(guò).
看看目錄,引入es6-promise就可以用了.
目錄
1.網(wǎng)絡(luò)請(qǐng)求 wxRequest.js
這里只寫了get和post.
我經(jīng)常會(huì)在網(wǎng)絡(luò)請(qǐng)求的時(shí)候用微信原生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) }) } } //無(wú)論promise對(duì)象最后狀態(tài)如何都會(huì)執(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 }) ); }; /** * 微信請(qǐng)求get方法 * url * data 以對(duì)象的格式傳入 */ function getRequest(url, data) { var getRequest = wxPromisify(wx.request) return getRequest({ url: url, method: 'GET', data: data, header: { 'Content-Type': 'application/json' } }) } /** * 微信請(qǐng)求post方法封裝 * url * data 以對(duì)象的格式傳入 */ 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) }) } } //無(wú)論promise對(duì)象最后狀態(tài)如何都會(huì)執(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)用場(chǎng)景很多,下面是promise最基本的用法,在then()中returnpromise對(duì)象.
這樣有效解決了回調(diào)嵌套的問(wèn)題.讓代碼看起來(lái)更優(yōu)雅.可讀性更高.
var util = require('../../utils/util') var wxApi = require('../../utils/wxApi') var wxRequest = require('../../utils/wxRequest') import config from '../../utils/config' //獲取應(yīng)用實(shí)例 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.獲取綁定手機(jī)號(hào)碼 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() }) } })
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持創(chuàng)新互聯(lián)!