衆所周知在小程序裏若是開啓了ES6轉ES5是無法用 async/await
的,但仍是有解決辦法的,搜到的解決方案都指向 runtime.js
這個文件。javascript
可是Facebook在19年3月8號更新了runtime.js
的代碼,用了 Function
函數,因而小程序裏又不能用了,驚不驚喜。。。java
固然是把 Function
改回去,我已經改好並上傳到Github了。git
有須要的直接下載便可 點我下載runtime.js (藍字可點,或者複製下方連接)。github
https://github.com/xubaifuCode/regeneratorRuntime/tree/master
複製代碼
爲了封裝request,簡易代碼以下小程序
new Promise(async (resolve) => {
const result = await request(options);
resolve(result);
});
複製代碼
運行時發現錯誤(編輯器開啓了ES6轉ES5)安全
網上搜,都說是下載regenerator-runtime的庫,例如bash
https://developers.weixin.qq.com/community/develop/doc/000a4ac7c7c108998916efda35b400
https://blog.csdn.net/sinat_33184880/article/details/85533095
複製代碼
可是我引入以後出現了以下錯誤async
VM9914:1 thirdScriptError
sdk uncaught third Error
Function(...) is not a function
TypeError: Function(...) is not a function
複製代碼
錯誤緣由是使用了 Function
函數, 小程序固然是拒絕使用這個不安全的函數的,因而就報錯了。編輯器
既然在小程序裏是不能使用 Fcnction
函數的,那爲何網上的解決方案都是這個呢?函數
因而我去GitHub倉庫查看runtime.js
的版本更新記錄。
https://github.com/facebook/regenerator/commits/master/packages/regenerator-runtime/runtime.js
複製代碼
發現3月8號有一次更新,點進去就發現,新增的代碼正是致使小程序報錯的罪魁禍首。
修復這個問題也是很簡單,刪掉新增的try-catch語句,同時將var runtime
改回var regeneratorRuntime
便可。
從上圖中能夠看到,是爲了不嚴格模式出錯才用的 Function
,仍是大佬姿式多,學習一個。