微信小程序开发两个月了.大家的项目都在不断迭代.已经不是小程序.这时候就会遇到多层回调嵌套的问题.有些目不忍视了.迫不得已引入es6-promise.在微信小程序内测的时候promise不需要手动引入,后来被微信移除了.看看效果.
	
promise详细的介绍我就不说了.有很多大神写过.
看看目录,引入es6-promise就可以用了.
	
1.网络请求 wxRequest.js
这里只写了get和post.
我经常会在网络请求的时候用微信原生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对象最后状态如何都会执行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}新闻热点
疑难解答