微信小程序使用Promise簡化回調
Promise 是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調函數(shù)和事件——更合理和更強大。它由社區(qū)最早提出和實現(xiàn),ES6 將其寫進了語言標準,統(tǒng)一了用法,原生提供了Promise對象。
所謂Promise,簡單說就是一個容器,里面保存著某個未來才會結束的事件(通常是一個異步操作)的結果。從語法上說,Promise 是一個對象,從它可以獲取異步操作的消息。Promise 提供統(tǒng)一的 API,各種異步操作都可以用同樣的方法進行處理。
在項目中,會出現(xiàn)各種異步操作,如果一個異步操作的回調里還有異步操作,就會出現(xiàn)回調金字塔。
比如下面這種
// 模擬獲取code,然后將code傳給后臺,成功后獲取userinfo,再將userinfo傳給后臺
// 登錄
wx.login({
success: res => {
let code = res.code
// 請求
imitationPost({
url: '/test/loginWithCode',
data: {
code
},
success: data => {
// 獲取userInfo
wx.getUserInfo({
success: res => {
let userInfo = res.userInfo
// 請求
imitationPost({
url: '/test/saveUserInfo',
data: {
userInfo
},
success: data => {
console.log(data)
},
fail: res => {
console.log(res)
}
})
},
fail: res => {
console.log(res)
}
})
},
fail: res => {
console.log(res)
}
})
},
fail: res => {
console.log(res)
}
})
下面分析如何用Promise來進行簡化代碼
因為微信小程序異步api都是success和fail的形式,所有有人封裝了這樣一個方法:
promisify.js
module.exports = (api) => {
return (options, ...params) => {
return new Promise((resolve, reject) => {
api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);
});
}
}
先看最簡單的:
// 獲取系統(tǒng)信息
wx.getSystemInfo({
success: res => {
// success
console.log(res)
},
fail: res => {
}
})
使用上面的promisify.js簡化后:
const promisify = require('./promisify')
const getSystemInfo = promisify(wx.getSystemInfo)
getSystemInfo().then(res=>{
// success
console.log(res)
}).catch(res=>{
})

getSystemInfo
可以看到簡化后的回調里少了一個縮進,并且回調函數(shù)從9行減少到了6行。
回調金字塔的簡化效果
那么再來看看最開始的那個回調金字塔
const promisify = require('./promisify')
const login = promisify(wx.login)
const getSystemInfo = promisify(wx.getSystemInfo)
// 登錄
login().then(res => {
let code = res.code
// 請求
pImitationPost({
url: '/test/loginWithCode',
data: {
code
},
}).then(data => {
// 獲取userInfo
getUserInfo().then(res => {
let userInfo = res.userInfo
// 請求
pImitationPost({
url: '/test/saveUserInfo',
data: {
userInfo
},
}).then(data => {
console.log(data)
}).catch(res => {
console.log(res)
})
}).catch(res => {
console.log(res)
})
}).catch(res => {
console.log(res)
})
}).catch(res => {
console.log(res)
})

簡化回調
可以看到簡化效果非常明顯。
同樣適用于網(wǎng)頁或者nodejs等中。
參考
源代碼
tomfriwel/MyWechatAppDemo 的promisePage頁面
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
將函數(shù)的實際參數(shù)轉換成數(shù)組的方法
實際參數(shù)在函數(shù)中我們可以使用 arguments 對象獲得 (注:形參可通過 arguments.callee 獲得),雖然 arguments 對象與數(shù)組形似,但仍不是真正意義上的數(shù)組。2010-01-01
JavaScript與Div對層定位和移動獲得坐標的實現(xiàn)代碼
JavaScript與Div對層定位和移動獲得坐標的實現(xiàn)代碼,需要的朋友可以參考下。2010-09-09
基于JQuery+HTML+JavaScript實現(xiàn)地圖位置選取和地址模糊查詢
本文詳細講解了如何使用 JQuery+HTML+JavaScript 實現(xiàn)移動端頁面中的地圖位置選取功能,本文逐步展示了如何構建基本的地圖頁面,如何通過點擊地圖獲取經(jīng)緯度和地理信息,以及如何實現(xiàn)模糊查詢地址并在地圖上標注,感興趣的小伙伴跟著小編一起來看看吧2024-07-07

