카테고리 없음

[2. React.js]Async/Await

로그앤 2023. 6. 16. 18:00

// Fetch the JSON file and assign to array

let DATA
DATA = fetch(filePath)
  .then((response) => response.json())
  .then((jsonData) => {
    // Access and use the data as needed
    console.log(jsonData)
    // OR call myFunction()
    myFunction(jsonData)
    return jsonData.map((item) => {
      // map each data
      return {
        name: item.name,
        title: item.title,
        email: item.email,
        role: item.role,
      }
    })
  })
  .catch((error) => {
    console.error("Error fetching JSON:", error)
  })
function myFunction(data) {
  console.log("Use JSON data outside of fetch scope", data)
}