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 evaluate promises inside an array.map()

The code below is self explanatory. I have an array of validator functions coming in which can be either synchronous or async. I want to map over that array, await any promise that's required and return an array that contains all the results of the validator functions. I don't seem to be able to do this with the native .map()

const syncValidator = () => 'Valid';

const asyncValidator = async () => {
    await new Promise(resolve => setTimeout(resolve, 1000));
    return "Async validation error";
};


const validations = [syncValidator, asyncValidator];

// Function to find the results of all the validations

const validateSchema = async (validatorArray, inputValue) => {
    const results = await validatorArray.map(
        async validationFunc => {
            const validation = await validationFunc(inputValue);
            return validation;
        }
    );

    return results;
}

(async () => {
    const ans = await validateSchema(validations, 'blahblah');

    // Expected answer: ['Valid', 'Async validation error']
    console.log("Ans: ", ans)

    // Incorrect answer: [ Promise {}, Promise {} ] 
})();
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!