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

95
Views
how to implement promise.allSettled in Javascript

I have this code to search if a user is in the list or not. The thing is, I need to implement the calls to findOne in parallel. How can I add the promise.allSettled?

As is the code, how can I create multiple calls to findOne? I have reviewed the documentation of the Promises but I don't know how to implement it in this case.

const findOne = (list, { key, value }) => {  
    return new Promise ((resolve, reject) => {  
        const element = list.find(element => element[key] === value); 
        
        setTimeout(() => {  
            element 
            ? resolve(element.name) 
            :reject(new Error('ERROR: Element not found')); 
        }, 2000); 
    });
  }
  
  const users = [  
  {
    name: 'Carlos', 
    rol: 'Teacher' 
  },
  {
    name: 'Ana', 
    rol: 'Boss' 
  }
  ];


console.log('findOne User'); 

async function fetchData(){ 
  try { 
    const findUser = await findOne(users,{key: 'name', value: 'Carlos'}); 
    console.log("user:" + findUser); 
  }catch(err){ 
    console.log(err.message); 
  }
    
}

fetchData(); 

I have this code to search if a user is in the list or not. The thing is, I need to implement the calls to findOne in parallel. How can I add the promise.allSettled?

As is the code, how can I create multiple calls to findOne? I have reviewed the documentation of the Promises but I don't know how to implement it in this case.

I don't know how to try it anymore... thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You would then want to pass a number of names to find to fetchData, and map those names to calls to findOne. Then call Promise.allSettled on the promises that you get back from that mapping. Finally deal with the returned settled-array as you wish:

const findOne = (list, { key, value }) => new Promise ((resolve, reject) => {  
    const element = list.find(element => element[key] === value); 
    setTimeout(() => element ? resolve(element.name) 
                             : reject(new Error('ERROR: Element not found'))
    , 2000); 
});
  
const users = [{name: 'Carlos', rol: 'Teacher'}, {name: 'Ana',  rol: 'Boss'}];

console.log('findOne User'); 

async function fetchData(names) {
  const promises = names.map(value => findOne(users, {key: 'name', value }));
  let results = await Promise.allSettled(promises);
  console.log(results);
}

fetchData(["Carlos", "Helen", "Ana"]);

about 4 years ago · Juan Pablo Isaza 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!