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

107
Vistas
Running program when catch occurs

I need to download images using this package - but sometimes, one of these images is corrupted. Here, one of the two sample images is corrupted and I'm adding a Math.random() to simulate this case. I'm now trying to restart the program once the .catch() occurs - the thing is the program runs infinitely then. How can I solve this?

My code is:

let imgArrays = ["https://upload.wikimedia.org/wikipedia/commons/d/dc/Gigi1.jpg", "https://mms.businesswire.com/media/20211119005773/fr/929256/5/Aries_Linden_mechanical_completion_11-19-2021.jpg"]

const runingImage = () => {


    const url = imgArrays[Math.floor(imgArrays.length * Math.random())];
    const path = 'img/image.jpg'
    options = {
        url: url,
        dest: path
    }
    download.image(options)
        .then(({
            filename
        }) => {
            console.log('Saved to', filename)
        }).catch(
            (err) => {
                console.log("running again");
                return runingImage();
            })
}

runingImage();

When I run it, it now only gives me:

running again
running again
...
Until I stops the console.
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Remove the return statement. It should be like:

let imgArrays = ["https://upload.wikimedia.org/wikipedia/commons/d/dc/Gigi1.jpg", "https://mms.businesswire.com/media/20211119005773/fr/929256/5/Aries_Linden_mechanical_completion_11-19-2021.jpg"]
    
const runingImage = () => {
  const url = imgArrays[Math.floor(imgArrays.length * Math.random())]
  const path = 'img/image.jpg'
  options = {
      url: url,
      dest: path
  }
  download.image(options)
    .then(({
        filename
    }) => {
        console.log('Saved to', filename)
    }).catch(
        (err) => {
          console.log("running again")
    })
}

runingImage()
about 4 years ago · Juan Pablo Isaza Denunciar

0

It is unfortunate that you stepped into a trap all programmers have fallen (and still falling) at some point: missing files/folders

when you are still in the development stage, always print errors in catch statements. this is what you would get and eventually solve the problem yourself:

console.log(err)

ENOENT: no such file or directory, open 'img/image.jpg'

also there is this little mistake you are doing. you are creating a void type function and try to return its value which is always undefined

solution:

  1. create an img folder in the directory you put this file of yours.
  2. remove return word inside catch block leaving only runingImage();
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