0

I am trying to get the PromiseResult from a JSON i fetch on JS.

async function load(addr){
  var groups = [];
  groups = await fetch(addr).then(response => response.json()).then(data => data[1]['group']);


  return groups

Output from promise

The First Pending Output promise is the second element of the output array. All i need here in the result value and not the whole object.

Any advise would be appreciated :)

the whole code

sync function load(addr){
  var groups = [];
  var myarray = []
  groups = await fetch(addr).then(response => response.json()).then(data => myarray.push(data));


  return groups
}


export default function (groupCount = 6, itemCount = 6, daysInPast = 30) {
  const L = load('../test.json');
  console.log(L)
  let groups = []
  for (let i = 0; i< groupCount; i++) {
    groups.push({
      id: `${i + 1}`,
      title: fetch('../test.json').then(response => response.json()).then(data => data[i]['group']),
  })
}

let items = []
for (let i = 0; i < itemCount; i++) {
  items.push({
    id: i + '',
    group: faker.random.number({ min: 1, max: groups.length }) + '',
    title: fetch('../test.json').then(response=>response.json()).then(data=>data[i]['title']),
    start: Date.parse(fetch('../test.json').then(response=>response.json()).then(data=>data[i]['start date'])),
    end: Date.parse(fetch('../test.json').then(response=>response.json()).then(data=>data[i]['end date']))
  })
}

items = items.sort((a, b) => b - a)

  console.log({ groups, items })

  return { groups, items }
}

0 Answers0