How to Loop HTTP Requests Until Some Condition Is Met

Published Jun 10, 2020  ∙  Updated May 2, 2022

I recently ran into an issue where I needed to keep sending HTTP requests until I had some valid response.

“Valid” means something different in different contexts.

Sometimes “valid” is just when status === 200, but other times, it’s something completely different.

I needed to fetch data from an API, but it was returning inconsistent responses.

Let’s say, for this example, our data ( returns some JSON object that looks like this:

    results: []

I wanted to keep making requests until results was populated.

This is what I did.

const sendRequest = async (query) => {
  const options = {
    url: `${API_URL}?q=${query}`, // Some valid API URL
    method: "GET" // Perform GET request
  return new Promise((resolve, reject) => {
    const request = (retries) => {
      // Make the HTTP request
      axios(options).then((res) => {
          // Check some condition based on response
          // Check number of retries left
          if ( > 0 && retries > 0) {
          } else {
            return resolve(;
      }).catch((error) => {

I wrapped my axios request inside a request() method, which takes in the number of retries I have for the current call.

When the axios callback gains program control, I check that is populated and that I have some retries left.

If is empty or I’ve used all my attempts, then I just resolve with the JSON object.

At the bottom, I called request(5), giving myself five attempts to retrieve desirable results.