'Typescript pattern - dealing with awaits that can be return void or empty
I'm curious if there is a tidy pattern approach I can use for this common scenario:
I have a function that gets a price from making an api call:
let currentPrice = await getCurrentPrice(prodId)
This getCurrentPrice() function in the ideal case makes the api call as usual and returns a string. Promise<string>.
async function getCurrentPrice(pId: string): Promise<string> {
axios({ .... })
.then(res => return res.data.price)
.catch(err => return "")
}
But in the error case I then need to write more logic at the point I made the call like if currentPrice === "".
This doesn't seem very tidy (mostly returning empty strings) hence wondering if there is a clearer pattern better used.
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
