缓存promise技术不错哦

1、 pageManager.js

const promiseList = {};
module.exports = {
  putData: function(pageName,data){
    promiseList[pageName] = data;
  },
  getData: function(pageName)
  {
    return promiseList[pageName];
  }
}

2、 util

const fPromise = fn => obj =>{
  return new Promise((resolve,reject)=>{
    obj.complete = obj.success = res=>{
      resolve(res)
    }
    obj.fail = err =>{
      reject(err)
    }
    fn(obj);
  })
}

module.exports = {
  fPromise
}

3、使用   小程序跳转之前先请求数据,加快跳转页的数据加载速度。

 

起始页:

onNavigate: function(){
   const that = this;
   const promiseBoos = fPromise(wx.request)({
    url:'https://'
  }).then(function(res){
   return res.data;  
  });  
},
gotoPageC: function(){
    this.onNavigate();
    wx.navigateTo({url:'..pageC/index'});
}

 跳转页:

onLoad: function(){
   
    const bookPromise = pageManager.getData('getBooks');
    if (bookPromise)
   {
       const resData  = bookPromise.then(
                function(data)
                { 
                      console.log(“需要渲染的数据是:”,data):
                },
                function(){
                     console.log("初始化失败”);
                } 
       )
   } 
}  

 

posted @ 2019-08-26 19:33  MvloveYouForever  阅读(415)  评论(0编辑  收藏  举报