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

146
Views
How to assign for loop promise result to the outside variable in Expressjs, Typescript?
export const getSObjectFields = async (request: Request, response: Response) => {
if (!request.body) { return response.status(401).json({ error: 'No SObject Selected' }); }
let selectedSObject = request.body;
let sObjectsDetails: any = [];
selectedSObject.forEach((element: string, _index: number) => {
    let promiseDescribeSObject = describeSObject(request, response, element);
    promiseDescribeSObject.then((res) => {
        sObjectsDetails.push(res);
    }).catch((err) => {
        console.error('Error: ', err);
    });
});
return response.status(200).json(sObjectsDetails);

}

I do have a function that call an API with parameter describeSObject(request, response, 'objectName'). I want to iterate the describeSObject function and save the result to the outside variable of for loop.

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use async/await syntax and use map and Promise.all to resolve all promises of the array together.

export const getSObjectFields = async (request: Request, response: Response) => {
  if (!request.body) { return response.status(401).json({ error: 'No SObject Selected' }); }
  let selectedSObject = request.body;
  let sObjectsDetails = await Promise.all(selectedSObject.map(async (element: string) => await describeSObject(request, response, element)))
  return response.status(200).json(sObjectsDetails);
}

almost 4 years ago · Santiago Trujillo Report
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!