前言
在平常的項目開發中肯定會遇到同步異步執行的問題,還有的就是當執行某一個操作依賴上一個執行所返回的結果,那么這個時候你會如何解決這個問題呢;
1.是用settimeout讓它異步執行,顯然這只是讓它加入異步任務隊列中去執行,但并不能保證等待其返回結果再去執行另一個操作。
2.還是自己封裝callback函數?那樣就會陷入所謂的回調地獄,代碼層層嵌套,環環相扣,邏輯稍微復雜就會很難去維護。
3.當然es6中的promise倒是很好的解決了這樣的問題,再配合es7的async和await就更完美了,await返回的也是一個promise對象,這個關于promise和async,await的使用方法就不說了。
實現方案
首先小程序目前還是不支持es7的async和await的,那么如何讓它支持呢
1、點擊下載
regenerator,并把下載好的runtime.js文件夾放到自己小程序的utils目錄下,包總共才20kb多,體積很小的。
2、在需要調的地方引入 import regeneratorRuntime from '../../utils/runtime.js'
3、如何封裝并使用
封裝:
const postData = async function(url, data) { wx.showLoading({ title: '加載中', }) let promiseP = await new Promise(function(resolve, reject) { wx.request({ url: baseUrl + url, data: data, method: 'POST', header: { 'content-type': 'application/json', 'access-token': wx.getStorageSync('token') }, success: function(res) { wx.hideLoading(); if (res.statusCode === 200) { resolve(res) } else { reject(res.data) } }, fail: function(err) { wx.hideLoading(); reject(err) if (err.code === 401) {} } }) }) return promiseP}module.exports = { postData}
使用:
import regeneratorRuntime from '../../utils/runtime.js';const app = getApp(), postData = require('../../service/koalaApi.js');async demo() { await postData(app.globalData.baseUrl + '/test',{ data: {} }).then((res) => { console.log(res) })}
下面進行了更完善的一個封裝,包括各種錯誤判斷的處理和簡化,通過傳參的方式,來靈活調用
// 當前hostconst url_host = require('API.js').host // 當前版本const currentVersion = require('util.js').currentVersion // 當前路徑import { currentPagePath } from 'util.js' // 調用fetch方法,然后依次鏈式傳入// url, method, header, data, loading(是否顯示loading) function fetch(url, method, header, data, loading) { // 判斷給服務端傳遞undefined的問題 let fetchP = new Promise(function (resolve, reject) { if (loading) { wx.showLoading({ icon: 'loading' }) } if(data && data.unionId && typeof data.unionId === "undefined"){ wx.hideLoading() return reject({ ok:false, error: 'unionId -> ' + typeof data.unionId }); } wx.request({ url: url_host + url, method: method ? method : 'GET', header: { 'content-type': 'application/json', // 默認值 'version': currentVersion, 'pagePath': currentPagePath() }, data: data, success: function (res) { if (res.statusCode < 500) { resolve(res.data) } else { showError() reject(res.data) } }, fail: function (err) { showError() reject(err) }, complete: function (comp) { if (loading) { wx.hideLoading() } } }) }) return fetchP}// 服務器開小差了function showError () { wx.hideLoading() // 獲取頭文件路徑 wx.navigateTo({ url: '/pages/serverError/serverError', })}module.exports = { fetch}
思考
1、為什么引入regeneratorRuntime,就能夠使用async/await?不需要配合babel嗎?
2、regeneratorRuntime都做了什么?
總結
1、首先先明白babel和polyfill分別干啥的;
Babel 是一個廣泛使用的轉碼器,Babel 默認只轉換新的 JavaScript 句法,而不轉換新的 API。
例如,Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局對象,以及一些定義在全局對象上的方法(比如 Object.assign)都不會轉譯。
如果想使用這些新的對象和方法,必須使用 babel-polyfill,為當前環境提供一個墊片。
2、Polyfill用于實現瀏覽器并不支持的原生API的代碼。
3、在明白上面的意思之后,還需要明白的是,babel-polyfill是一股腦把全部都給你添加到js文件中,而現在的runtime將會判斷你哪些需要加載的,有選擇性的進行加載,并且后者也不會污染全局變量。在這里regeneratorRuntime最終轉化成es6的generator來用的。具體的可以自己去下babel官網,輸入相關代碼可以看下最終轉換后的代碼。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答