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

315
Vistas
How can I set a flag from a promise that is contained in a loop, pre-ES8?

What I am trying to achieve is sending a bunch of post requests to a print service, and if any of them fail I want to display a warning message to the user. My current set up has a warning message for each time it fails.

items.forEach(function (item) {

    var data = {
        date: self.getDateTime(),
        barcode: item.barcode,
        description: item.description
    };
    var url = 'http://localhost/print_service'
    $.ajax({
        method: "POST",
        url: url,
        data: JSON.stringify(data),
    }).fail(function () {
        self.display_warning(
            'Failed to connect to printer',
        );
    });
});

I am not able to make use of async / await in this project.

My thought was to just set some flag like printFailed = true, and after the loop just display a message if it is true. However, the fail is of course asyncronous, so the flag is now set when I make that check.

How can I tackle this effectively? Typically I would throw the error and / or flag into a .then(), but I can't do this as it would then still be caught in the loop.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try to use Promise.all:

const requests = items.map(function(item) {
    return $.ajax({
        method: "POST",
        url: 'http://localhost/print_service',
        data: JSON.stringify({
            date: self.getDateTime(),
            barcode: item.barcode,
            description: item.description
        }),
    });
});

Promise.all(requests).catch(function() {
    self.display_warning('Failed to connect to printer', );
});


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