利用 高阶函数 | 作用域 | 配置初始化脚本 的全局变量
import context from '@utils/context'; const configFileUrl = `http://sgs.com/api`; const timtOut = proObj => { return Promise.race([ proObj, new Promise(resolve => { setTimeout(() => { resolve({ code: 1001, errMsg: '接口请求超时,时限3秒' }); }, 3000); }) ]); }; const fetchApiPro = requestPromise({ url: configFileUrl }); const h_fetchConfigFile = () => { let apiData = null; fetchApiPro.then(res => (apiData = res)); return () => apiData; }; const output = { init() { this._getConfigFile = h_fetchConfigFile(); }, getPreSaleConfig: async function () { const { preSaleListConfig } = this; if (preSaleListConfig) { return preSaleListConfig; } let res; try { res = await timtOut(fetchApiPro); } catch (e) { res = { code: 1001, errMsg: e }; } return res; }, get preSaleListConfig() { return output._getConfigFile(); } }; export default output;