0

Im trying to return the value itemIDsList but it shows up in the console as undefined?

function getAllData() {
    fetch("JSONitemIDsList.json")
    .then(response => response.json())
    .then(data => {
        var itemIDsList = data.ids
    })
    return itemIDsList

}
var itemIDsList = getAllData()
console.log(itemIDsList)

JSON file:

{
    "ids": [
        "sw0001a",
        "sw0001b",
         ....
         ...
         ..
         .
]}
logan9997
  • 73
  • 1
  • 5
  • You need to return itemIDsList from inside the then. Like .then(data => { var itemIDsList = data.ids return itemIDsList }) – Anil Dec 22 '21 at 18:19
  • @Anil still not going to make any difference since it is asynchronous. – epascarello Dec 22 '21 at 18:21
  • oh my bad... didn't see the function invoke You might want to return a promise from getAllData and resolve it from then(). and call it like getAllData().then(data => {}) or can use aync-await – Anil Dec 22 '21 at 18:24

0 Answers0