Star Wars Planets
getPlanets
function fetches a list of planets from the Star Wars API and returns an array containing the names of the first 5 planets. Rewrite the function to use Promises instead of async/await
.Signature
getPlanets(): Promise<string[]>
Instructions:
fetch
function to make a GET request to the Star Wars API endpoint: https://swapi.dev/api/planets/
.json()
method.results
array of the response data, and return them as an array of strings.If any errors occur during the request or response parsing, throw an error with a descriptive message.
Example:
getPlanets()
.then(names => console.log(names))
.catch(error => console.error(error));
// Output: ["Tatooine", "Alderaan", "Yavin IV", "Hoth", "Dagobah"]