-1
public async getOfferingPricingPlan({
    offeringId
  }: GetOfferingPricingPlanOptions): Promise<GetOfferingPricingPlanResponse> {
    return this.offeringClient.getPricingPlan({
      offeringId: offeringId,
      auth: {
        asap: this.asap
      }
    });
  }
}
     let pricingPlanId;

    this.getOfferingPricingPlan({
      offeringId: mapItemToOfferingId(item),
    }).then(res=>{
      if(res.values && res.values[0] && res.values[0].key){
      return pricingPlanId = res.values[0].key;
      } else {
        throw ("Invalid ")
      }
    
    });

console.log(pricingPlanId);
    

I need pricingPlanId value outside asyn function

Raul Rene
  • 10,014
  • 9
  • 53
  • 75

1 Answers1

0

Well because it's a Promise you only have the value once the promise executes.

You can add an async keyword to the wrapping function so you can use await inside.

async function() {

    // ...

    const pricingPlanId = await this.getOfferingPricingPlan({
      offeringId: mapItemToOfferingId(item),
    }).then(res=>{
      if(res.values && res.values[0] && res.values[0].key){
      return pricingPlanId = res.values[0].key;
      } else {
        throw ("Invalid ")
      }
    
    });

    console.log(pricingPlanId);
}

OR as deceze pointed out in the comments -> it's not ideal to mix .then and await, so you can drop then altogether:

async function() {
    let pricingPlanId;
    const res = await this.getOfferingPricingPlan({
      offeringId: mapItemToOfferingId(item),
    })

    if(res.values && res.values[0] && res.values[0].key){
       pricingPlanId = res.values[0].key;
    } else {
       throw ("Invalid ")
    }
}

Raul Rene
  • 10,014
  • 9
  • 53
  • 75