若是新接触 Promise 的话,在网上能找到不少介绍 Promise 及其使用的文章(好比:ECMAScript 6 入门 / Promise 对象),这里就不赘述了,简而言之就是用来处理异步调用的一大利器。小程序
微信小程序的API均可以传入函数 success,fail 和 complete 来实现异步回调。微信小程序
样例一promise
// 显示”载入中”,在一秒后消失 wx.showLoading({ title: "载入中", success: function () { setTimeout(function () { wx.hideLoading() }, 1000) }, fail: function(){}, complete: function(){} });
原生的 success,fail 和 complete 已可以知足基本的异步回调了,可是若是遇到多个连续的阻塞任务,会形成多层嵌套(如样例二所示),就很奔溃。微信
样例二异步
// 显示“保存中”,一秒后隐藏,半秒后显示“载入中”,一秒后隐藏 wx.showLoading({ title: "保存中", success: function () { setTimeout(function () { wx.hideLoading({ success: function () { setTimeout(function () { wx.showLoading({ title: "载入中", success: function () { setTimeout(function () { wx.hideLoading() },1000) } }) }, 500) } }) }, 1000) } })
上面的例子有七个阻塞任务:显示“保存中”,停顿一秒,隐藏,停顿半秒,显示“载入中”,停顿一秒,隐藏。从直觉上来思考,这些任务应该是以队列的形式存在,一个完成了再开始下一个,而非层层嵌套,这也是使用Promise的一大缘由,能够链式调用。ide
上面的例子若是用Promise封装以后的API来写,看起来就很是直观(样例三)函数
样例三this
wsAPI.taskSequence() .then(() => wsAPI.showLoading({title: "保存中"})) .then(() => wsAPI.sleep(1000)) .then(() => wsAPI.hideLoading()) .then(() => wsAPI.sleep(500)) .then(() => wsAPI.showLoading({title: "载入中"})) .then(() => wsAPI.sleep(1000)) .then(() => wsAPI.hideLoading()) .then(() => console.log("done"))
注: (A)=>{B} 是 ES6 的箭头函数,至关于 function(A){B},箭头函数不用显式 return。spa
好比 () => 5 就会 return 5code
console.log((() => 5)()) // 5
封装实现
wsAPI的源代码实现以下:
let nullFn = () => { }; function IllegalAPIException(name) { this.message = "No Such API [" + name + "]"; this.name = 'IllegalAPIException'; } let services = { sleep: (time) => { return new Promise(function (resolve, reject) { setTimeout(resolve, time); }) }, stop: () => { return new Promise(function (resolve, reject) { }) }, taskSequence: () => { return new Promise(function (resolve, reject) { resolve() }) } }; export let wsAPI = new Proxy(services, { get: function (target, property) { if (property in target) { return target[property]; } else if (property in wx) { return (obj) => { return new Promise(function (resolve, reject) { obj = obj || {}; obj.success = (...args) => { resolve(...args) }; obj.fail = (...args) => { reject(...args); }; obj.complete = nullFn; wx[property](obj); }); } } else { throw new IllegalAPIException(property); } } });
wsAPI 用 Proxy(ECMAScript 6 入门 / Proxy)从新封装了 wx 的全部API。并新增了 sleep ,stop 和 taskSequence。sleep 用于阻塞一段时间;taskSequence 是一个空的 Promise,让代码看起来更整齐美观,可读性更好(样例四);stop 用于中止任务序列进行下去(样例五)
样例四
// taskSequence wsAPI.taskSequence() .then(() => wsAPI.showLoading({title: "保存中"})) .then(() => wsAPI.sleep(1000)) .then(() => wsAPI.hideLoading()) .then(() => wsAPI.sleep(500)) .then(() => wsAPI.showLoading({title: "载入中"})) .then(() => wsAPI.sleep(1000)) .then(() => wsAPI.hideLoading()) .then(() => console.log("done")) // 没有 taskSequence,第一个promise就和下面的不对齐 wsAPI.showLoading({title: "保存中"}) .then(() => wsAPI.sleep(1000)) .then(() => wsAPI.hideLoading()) .then(() => wsAPI.sleep(500)) .then(() => wsAPI.showLoading({title: "载入中"})) .then(() => wsAPI.sleep(1000)) .then(() => wsAPI.hideLoading()) .then(() => console.log("done"))
样例五
wsAPI.taskSequence() .then(() => wsAPI.showModal({title: "保存", content: "肯定保存?"})) .then(res => { if (!res.confirm) { return wsAPI.stop(); } }) .then(() => console.log("to save")) .then(() => wsAPI.showLoading({title: "保存中"})) .then(() => wsAPI.sleep(1000)) .then(() => wsAPI.hideLoading()) .then(() => console.log("done"))