0

I want dropVolume get the value to use it in another method after calling my getAllDropsVolumePerDate(date) method. but every time I get dropVolume = 0

 dropVolume = 0;
  getAllDropsVolumePerDate(date: string) {
    this.campaignService.getAllCampaigns().subscribe((res) => {
      res.forEach((campaign) => {
        campaign.drops.forEach((drop) => {
          if (drop.dropDate === date) {
            this.dropVolume = this.dropVolume +drop.dropVolume;
          }
        });
      });
     return this.dropVolume;
    });
    return this.dropVolume;
  }
Wahéb
  • 541
  • 1
  • 5
  • 15

1 Answers1

1

Subscription is asynchronous and it can take time to return a value (even if it's in milliseconds), so your subscription starts BUT your return line is out of it and will be triggered before the subscription returns with a value. Move your return line inside the subscription.

Misha Mashina
  • 1,739
  • 2
  • 4
  • 10