My code includes an async function that retrieves a value
async fetchUserName(environment: string, itemName: string, authToken: string): Promise<any> {
let result = await this.obtainDeviceName(environment, itemName, authToken);
return result;
};
In another part of my code, I am invoking the aforementioned function as follows:
let productName;
const prod = this.fetchUserName(environment, itemName, authToken).then((response) => {
productName = response;
this.wait(300);
});
Even though the variable `productName` appears to be functioning correctly and displaying the response in my log, when I attempt to use `productName` outside of this particular block, I encounter an issue where it is undefined.
promoDetails = {
name: productName,
startDate: start,
endDate: end
};
I am trying to assign `productName` to the `name` field within the `promoDetails` object, but unfortunately, I cannot seem to get it working as intended.
If anyone could provide insight into what may be causing this issue, I would greatly appreciate it. Thank you.