Javascript 使用承诺初始化数据

Javascript 使用承诺初始化数据,javascript,angular,promise,ionic3,Javascript,Angular,Promise,Ionic3,我真的很难处理数据初始化和承诺。我使用离子3进行角度和离子存储,但我的问题主要是关于承诺如何运作 基本上,我希望实现以下目标: 当我的应用程序启动时,它应该使用本地存储集合 如果本地存储集合不存在或为空,请使用http创建一个新集合 如果http失败,请使用本地数据创建一个集合 到目前为止,我的解决方案是: getNewsItems():Promise<any> { return this.storage.get(this.newsKey).then((data) =>

我真的很难处理数据初始化和承诺。我使用离子3进行角度和离子存储,但我的问题主要是关于承诺如何运作

基本上,我希望实现以下目标:

  • 当我的应用程序启动时,它应该使用本地存储集合
  • 如果本地存储集合不存在或为空,请使用http创建一个新集合
  • 如果http失败,请使用本地数据创建一个集合
到目前为止,我的解决方案是:

getNewsItems():Promise<any> {
  return this.storage.get(this.newsKey).then((data) => {
    if(data == null)
    { 
      return (this.buildNewsItemsViaHttp());
    } else {
      return (data);
    }
  });
}

private buildNewsItemsViaHttp(){
  return new Promise(resolve => {
    this.http.get('some/url/to/fetch/data')
      .subscribe(
        data => {
          this.newsCollection = data;
          this.storage.set(this.newsKey, this.newsCollection);
          resolve(this.newsCollection);
        },
        (err) => {
          resolve (this.buildNewsItemsViaLocalJSON());
        }
      );
});
}

private buildNewsItemsViaLocalJSON() {
  return new Promise(resolve => {
    this.http.get('assets/data/newsCollectionLocal.json')
      .subscribe(
        data => {
          this.newsCollection = data;
          this.storage.set(this.newsKey, this.newsCollection);
          resolve(this.newsCollection);
        },
        (err) => {
          console.log(err);
        }
      );
});}
getNewsItems():承诺{
返回this.storage.get(this.newsKey)。然后((数据)=>{
如果(数据==null)
{ 
返回(this.buildNewsItemsViaHttp());
}否则{
返回(数据);
}
});
}
私有buildNewsItemsViaHttp(){
返回新承诺(解决=>{
this.http.get('some/url/to/fetch/data')
.订阅(
数据=>{
this.newscolection=数据;
this.storage.set(this.newsKey,this.newcollection);
解决(此。新闻收集);
},
(错误)=>{
解析(this.buildNewsItemsViaLocalJSON());
}
);
});
}
私有buildNewsItemsViaLocalJSON(){
返回新承诺(解决=>{
this.http.get('assets/data/newscolectionlocal.json'))
.订阅(
数据=>{
this.newscolection=数据;
this.storage.set(this.newsKey,this.newcollection);
解决(此。新闻收集);
},
(错误)=>{
控制台日志(err);
}
);
});}
我不喜欢它的某些部分,例如在承诺中返回一个承诺——这真的是一个问题吗


提前感谢

一个干净的解决方案可以使用异步\await方法:

async buildNewsItemsViaHttp(){
   return await this.http.get()....
}

async buildNewsItemsViaLocalJSON(){
   return await this.http.get()....
}

async getNewsItems(){
   return await this.storage.get()...
}

async getItems(){
     return ((await this.getNewsItems()) || (await this.buildNewsItemsViaLocalJSON()) || (await this.buildNewsItemsViaHttp()));
}
用法:

const items = await this.getItems();
您可以优化资源,缓存它们并在每个函数中返回它们。 例如:


但是如果承诺应该遵循错误路径,会发生什么。除非在每个阶段都捕捉到错误,并且falsy值被强制进入成功路径,否则
(wait x()| |(wait y())|(wait z())
中的流将不会像您(通常)所希望的那样。将“测试1”和“测试2”的结果进行比较。@Roamer-1888-这不是生产代码,而是一段显示概念的代码。您应该使用try-catch来处理每个方法中的错误。此外,还可以修改
getItems()
方法,使其不使用或命令。
async buildNewsItemsViaHttp(){
   let result = cache.get(); // todo

   if(!result){
        result = await this.http.get()...
        cache.set(result)
   }

    return result;
}