Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
how to return object for every element in array inside asynchronous function
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));
  };
  //////////////////
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda