1
const Request = axios.create({
baseURL: 'https://ptx.transportdata.tw/MOTC/v2/'
});

export const ScenicSpotRequest = async () => Request.get('Tourism/ScenicSpot? 
$top=30&$format=JSON').then(response => response.data);

export async function getScenicSpotRequest() {
try {
      const item = await ScenicSpotRequest();
      // console.log(item);
      return item;
    } catch (err) {
      console.error(err);
    }
}

The value of return item is a promise. However I want the promise turn to Object which I can use it like getScenicSpotRequest[0].Name

1 Answers1

1

An async function will always return a Promise. A promise that it will give you the result/error whenever it itself gets it.

So if you want to use the data of the Promise you need to make use of .then()

For example, wherever you are using getScenicSpotRequest(), make use of .then to get the reusult.

getScenicSpotRequest().then(res => {
      console.log(res)
})
Rohan Agarwal
  • 2,441
  • 2
  • 18
  • 35