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

148
Views
Is there a way to make http requests while looping through an array and returning the array with the request result?

The question might seem silly and unclear because I essentially don't know what approach I should take in order to achieve this, but it will make more sense when I give examples.

So, lets say we have the following array:

const arr = [{key1: 'value1', key2: 'value2'}, {key1: '', key2: 'value2'}, {key1: 'value1', key2: 'value2'}]

As you can see, the item at index 1 has no value for the 'key1' key , but I can get this value from an http request.

What I'm ultimately trying to achieve is loop through the array, check if key1 has a value, if not make an http request to get said value and get a new array where all the indexes have a value for key1.

Something like this ( which obviously doesnt work )

const arr = [{key1: 'value1', key2: 'value2'}, {key1: '', key2: 'value2'}, {key1: 'value1', key2: 'value2'}];

arr.map(i => {
  if (!i.key1) {
    const value = getValueFromHttpRequest();
    
    return {
      ...i,
      key1: value
    }
  }
  
  return i;
})

So a working example of an array I would get would be

[{key1: 'value1', key2: 'value2'}, {key1: 'valueFromHttpRequest', key2: 'value2'}, {key1: 'value1', key2: 'value2'}]

I know it sounds confusing, but I appreciate any help I can get with this.

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

0

If you're inside an async function already, you can request them 1 at a time using a standard for loop

for (const el of arr) {
   if (el.key1) continue; // skip all elements that already have key1
   const response = await requestInfo();
   el.key1 = response.something; // this is what I mean by 'edit the array as you go'
   ...
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use Promise.all to get an array of responses.

const arr = [{key1: 'value1', key2: 'value2'}, {key1: '', key2: 'value2'}, {key1: 'value1', key2: 'value2'}];

const result = Promise.all(arr.map(async (i) => {
  const value = await sendRequest();
  
  return value;
}));

about 4 years ago · Juan Pablo Isaza Report

0

You can loop over the keys and for empty key1s you can make an API call and get the value.

const getKey1 = () =>
  new Promise((res) => setTimeout(() => res("value1")), 1000);

const keys = [
  { key1: "value1", key2: "value2" },
  { key1: "", key2: "value2" },
  { key1: "value1", key2: "value2" },
];

async function fillMissingKeys(keys) {
  return Promise.all(
    keys.map(async ({ key1, key2 }) => ({
      key1: !key1 ? await getKey1() : key2,
      key2,
    }))
  );
}

fillMissingKeys(keys).then(console.log);

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!