最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 6 - how to await the subscribe on a call - Stack Overflow

programmeradmin2浏览0评论

In the code below I have 2 calls, getDetail and Save. Save uses an object 'this.detail$' which is created in the subscribe of the 1st call, getDetail.

The trouble is this.detail$ is never assigned to before Save gets called. I've tried to put the Save logic in a function within the subscribe after this.detail$ is assigned to bit I get the same result. What's the best way to handle this?

SetPublishedFlag(flag: boolean, _id:string){
    this.id = _id;
    this.getDetail();
    this.Save(flag);
  }

  getDetail(): void {
    this.data.getEventDetail(this.id).subscribe(data => this.detail$ = data as DetailModel);
  }

  Save(flag: boolean) {
    this.detail$.Published = flag;
    this.edit.editEvents(this.detail$, this.id);
  }
}

In the code below I have 2 calls, getDetail and Save. Save uses an object 'this.detail$' which is created in the subscribe of the 1st call, getDetail.

The trouble is this.detail$ is never assigned to before Save gets called. I've tried to put the Save logic in a function within the subscribe after this.detail$ is assigned to bit I get the same result. What's the best way to handle this?

SetPublishedFlag(flag: boolean, _id:string){
    this.id = _id;
    this.getDetail();
    this.Save(flag);
  }

  getDetail(): void {
    this.data.getEventDetail(this.id).subscribe(data => this.detail$ = data as DetailModel);
  }

  Save(flag: boolean) {
    this.detail$.Published = flag;
    this.edit.editEvents(this.detail$, this.id);
  }
}
Share Improve this question edited Oct 1, 2021 at 8:29 Guerric P 31.9k6 gold badges58 silver badges106 bronze badges asked Oct 1, 2018 at 9:51 DarkW1nterDarkW1nter 2,86912 gold badges71 silver badges128 bronze badges 1
  • Why can't you call Save in subscribe rather than in SetPublishedFlag? – Ali Shahbaz Commented Oct 1, 2018 at 9:54
Add a ment  | 

3 Answers 3

Reset to default 3

Try something like:

async SetPublishedFlag(flag: boolean, _id:string){
    this.id = _id;
    this.detail$ = await this.getDetail();
    this.Save(flag);
  }

  getDetail(): Promise<DetailModel> {
    return this.data.getEventDetail(this.id).toPromise();
  }

  Save(flag: boolean) {
    this.detail$.Published = flag;
    this.edit.editEvents(this.detail$, this.id);
  }
}

Perhaps not the best solution, but a solution:

SetPublishedFlag(flag: boolean, _id: string){
    this.id = _id;
    this.getDetail(flag);
  }

  getDetail(flag): void {
    this.data.getEventDetail(this.id).subscribe((data) => {
      this.detail$ = data as DetailModel;
      this.Save(flag);
    });
  }

  Save(flag: boolean) {
    this.detail$.Published = flag;
    this.edit.editEvents(this.detail$, this.id);
  }

You can map the data back to your caller function and then subscribe over there to call Save().

or alternatively you can call the method Save() from getDetail() also.

SetPublishedFlag(flag: boolean, _id:string){
    this.id = _id;
    this.getDetail().subscribe(() => { 
             this.Save(flag); // call save after observable has been returned
     });
  }

  getDetail(): void {
    return this.data.getEventDetail(this.id).pipe(map(data => this.detail$ = data as DetailModel));
  }

  Save(flag: boolean) {
    this.detail$.Published = flag;
    this.edit.editEvents(this.detail$, this.id);
  }
}
发布评论

评论列表(0)

  1. 暂无评论