Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!