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

170
Views
Simplifying a for loop to find an item in an array (returned asynchronously) by condition in JavaScript

I have a function called getItems that returns an array of objects asynchronously. Each object has a isOccupied method that returns a boolean. I wrote a function that takes an index and returns whether index-th item in the array's isOccupied is true or false.

async itemIsOccupied(index) {
 return getItems().then(items => {
   if (items.length - 1 < index) return false;
   return items[index].isOccupied()
 });
}

This just works fine, but the fact that I have to use an async function to fetch the array makes it lengthy. Is there a way to simplify this?

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

0

I'd clean it up a little like this...

async itemIsOccupied(index) {
  const items = await getItems();
  return index < items.length && items[index].isOccupied();
}
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!