the promise creates an array like this, taken from the console:
(6) [Promise, Promise, Promise, Promise, Promise, Promise]
0: Promise {<fulfilled>: undefined}
1: Promise {<fulfilled>: undefined}
2: Promise {<fulfilled>: undefined}
3: Promise {<fulfilled>: undefined}
4: Promise {<rejected>: SyntaxError: Unexpected token < in JSON at position 0}
5: Promise {<fulfilled>: undefined}
length: 6
which cannot be used.
the code is this:
export default async function getData() {
let response = await request('http://localhost:2000/api/ves.json').then((data) => fetch(data));
let store = await response.json();
let list = await store.map(async (input, index)=>{
let details = await request(`http://localhost:2000/api/${input.id}.json`).then((data) => fetch(data));
let foo = await details.json();
console.log(foo);
input = await {...input, ...foo};
});
return list;
}
at the point, the list is returned (when the useData function is used) the mapping seems to have not finished. So it returns "promise" instead of the list it should.
The aim is to combine the objects. This is don't by using information from the first object (in an array of objects) to get information from the second object. Then pushing the second object into the first one at the same point in the array the information was retrieved to get the second object.
The same problem appears if I also do either
input = await {...input, ...foo};
}});
await Promise.all(list)
return list;
or
console.log(foo);
input = await {...input, ...foo};
}});
let fish = await Promise.all(list)
return fish;
gives the console error
(6) [undefined, undefined, undefined, undefined, undefined, undefined]
this useData function is called in a react page through this.
const [ves] = useData();
useEffect(()=>{
console.log(ves);
},[ves])
await Promise.all(list)should wait for all Promises in the mapped array.