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

104
Views
Promise.allSettled returns catch as fullfilled

I am making https requests using a function batchRequest that collects the promises in batches with Promise.allSettled and sends them to the apiRequest function that makes the actual calls with axios.

On the batchRequest function I send an array of urls to the HTTP request function and await the results. Then I use the Promise.allSettled status to identify the rejected ones. But when the apiRequest catches as error Promise.allSettled sees it as status: fullfilled

async function batchRequest(data, index) {
  console.log(`Running... ${index} round`)
  while (data.length) {

    // Batched request according to concurrent setting
    const batch = data
      .splice(0, settings.concurrent)
      .map((url) => apiRequest(url, index))

    const results = await Promise.allSettled(batch)

    results.forEach(({ status, value, reason }) => {
      if (status === 'fulfilled') {
        console.log(value) // I get both the try and catch here from `apiRequest`
      }
      if (status === 'rejected') {
        console.log(reason)
        // I never get anything here
      }
    })
  }
}

Api request function

async function apiRequest(url, index) {
  try {
    const { data } = await axios('https://api-end-point')
    return 'All good' + url
  } catch (error) {
    const status = error.response?.status ?? 'No response'
    return  `Error: ${status}, ${url}`
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Don't return the error. throw it!

async function apiRequest(url, index) {
  try {
    const { data } = await axios('https://api-end-point')
    return 'All good' + url
  } catch (error) {
    const status = error.response?.status ?? 'No response'
    throw  `Error: ${status}, ${url}` // <------------change here
  }
}
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!