Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
javascript - Fetch API until there is no data

I am using a restful API, and I need to post some data. but before, I need to ensure that the table is empty. The problem I am facing is that the delete method of this API return success, but does not immediately delete the data, this data is sent to a queue to delete asap.

So before I call the post method, I need to check if I have any items on this table. How can I call this method several times to ensure that the post method will only be executed when the table is empty?

async function run() {

    apiDelete()
    .then(() => console.log('deleting')) 
    .then(() => {

        getItems().
        then(() => {
            
            apiPost()
             .then(() => console.log('posting...'))
             .catch(e => console.log(e))
        })
       
      })
      .catch(e => console.log(e))     
     
  return

}

The methods:

async function apiPost() {
  const url = 'https://apipost.com/data/v2/1234'
  const options = {
    method: 'POST'
  }  
  return (await fetch(url, options)).json()
}

async function apiDelete() {
  const url = 'https://apidelete.com/data/v2/all'
  const options = {
    method: 'DELETE',    
  }
  return (await fetch(url, options)).json()
}

async function getItems() {
  const url = 'https://apiget.com/data/v2/all'
  const options = {
    method: 'GET',    
  }  
  return (await fetch(url, options)).json()
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to make a recursive call with an exit condition that asks it to exit when the number of elements is 0.

One way could be something like this:

async function deleteUntilEmpty() {
  await apiDelete();
  const items = await getItems();

  if (items.length) {
    return deleteUntilEmpty();
  }
}

async function run() {
  await deleteUntilEmpty();
  apiPost()
    .then(() => console.log("posting..."))
    .catch((e) => console.log(e));
}

Instead of deleting constantly, you might as well just poll to see if the server has completed the original delete:

async function pollUntilEmpty() {
  const items = await getItems();

  if (items.length) {
    // this could be a good spot to introduce some kind of 
    // sleep to not DDOS your server ;)
    return pollUntilEmpty();
  }
}

async function deleteAndPollUntilEmpty() {
  await apiDelete();
  await pollUntilEmpty();
}

async function run() {
  await deleteAndPollUntilEmpty();
  apiPost()
    .then(() => console.log("posting..."))
    .catch((e) => console.log(e));
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda