I have a function called fetchSavedCards that retrieves a list of saved cards. The function returns a Promise Object, but I want to convert it into an array of strings and return it. Is this possible?
const fetchSavedCards = (): string[] => {
return fetch("/fetchSavedCards")
.catch((error: Error) => {
throw new Error(error.message);
})
.then((response) => {
return response.json();
})
.then((cards: string[]) => {
return cards;
});
};
After retrieving the result set, I need to display it on a web page. However, I've encountered an issue where the .map() function does not work with a Promise object.
<CardContent>
<label> Card Selection </label>
<div className="container" id="cards">
{Service.fetchSavedCards().map(
(card) => <label>
<input type="radio" name="selectedCard" value="test" onChange={this.handleCardSelectionChange}/>
{card}
</label>)}
</div>
...
</CardContent>