首页 > 其他 > 详细

利用 高阶函数 | 作用域 | 配置初始化脚本 的全局变量

时间:2020-04-28 22:47:16      阅读:59      评论:0      收藏:0      [点我收藏+]
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;

 

利用 高阶函数 | 作用域 | 配置初始化脚本 的全局变量

原文:https://www.cnblogs.com/liujinyu/p/12797811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!