const fetchFavSchemas = schemas => {
return new Promise((resolve, reject) => {
schemas = schemas.map(e => {
try {
axios
.get(`MY_URL`)
.then(
res => res.data
// i want to return this << res.data >> object
// for every element in the array.
);
} catch (error) {
console.log(error);
reject(error);
}
});
resolve(schemas);
});
};
const getFavoritSchemas = () => {
fetchFavSchemas([7, 8]).then(res => console.log(res));
};
// i expect to return an array of arrays [[],[]]. // but i get array on undefineds [undefined , undefined]
Also when i want to work with useState hook and set the state on every element in the array But i get the initial value. I did some search on useState and i think it is not re-render on the same event and i have to do another trigger in order to get the current state. // please someone help me acheive what i want to do. // also help me understand how to get the current state in useState immediately after changing it (on the same click).
const [favSchemas, setFavSchemas] = useState([]);
const fetchFavSchemas = schemas => {
return new Promise((resolve, reject) => {
schemas.map(e => {
try {
axios
.get(`MY_URL`)
.then(res => setFavSchemas(prev => [...prev, ...res.data]));
} catch (error) {
console.log(error);
reject(error);
}
});
resolve();
});
};
const getFavoritSchemas = () => {
fetchFavSchemas([7, 8]).then(res => console.log(favSchemas));
};
//////////////////