-2

sorry, can someone show me the code and explain me, how to I can load only once the json file, and use all the content file OUTSIDE the function?

    let jsonCompleto
    
    fetch('uno.json')
    .then(function (response) {
        return response.json();
    })
    .then(function (data) {
        todo = data.length
        console.log(todo)
        jsonCompleto = data
        console.log(jsonCompleto) // this return all the json file correctly
    
    })

console.log(jsonCompleto) // this return undefined
// how can load only once the json file and use all content of json file OUTSIDE the function ?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
mirta
  • 1
  • 2
  • Please don't re-ask the exact same question when your previous attempts have already been closed – Phil Nov 09 '21 at 00:10

1 Answers1

-2

you can use async/await

"use strict";

const myFetch = async () => {
  const response = await fetch('https://jsonplaceholder.typicode.com/posts/1')
  return response.json()
}

const myFunc = async () => {
  data = await myFetch()
  
  console.log(data)
}

myFunc()

you can see the fetch free documetation

Phil
  • 157,677
  • 23
  • 242
  • 245