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

180
Vistas
Creating a Promise

Inside a Node controller method I have this code block that I would like to await for before continuing with the code that comes next. How can I do this?

I thought this should be done with a Promise (the only one in that controller method) and have tried the code below. But this generates the error below. What am I doing wrong?

TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))

const other = new Promise(async function () {
    updatedFields.last_login_at = newDateNow();
    if (updatedFields.isActivated) {
        updatedFields.activated_at = newDateNow();
        updatedFields.deactivated_at = null;
    } else if (updatedFields.isActivated === false) {
        updatedFields.deactivated_at = newDateNow();
        updatedFields.activated_at = null;
    }
});

await Promise.all(other);

... only when the Promise is completed, continue with the rest of the code
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When a promise is created, its state is pending, and when you await it, it waits until it is rejected or resolved. Since you don't resolve or reject, it does not work.

enter image description here

 const other = new Promise(async function (resolve,reject) {
    updatedFields.last_login_at = newDateNow();
    if (updatedFields.isActivated) {
        updatedFields.activated_at = newDateNow();
        updatedFields.deactivated_at = null;
    } else if (updatedFields.isActivated === false) {
        updatedFields.deactivated_at = newDateNow();
        updatedFields.activated_at = null;
    }
    resolve(updatedFields)
    });

await Promise.all([other]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

  1. Promise.all takes an array of promises!
  2. Use promise convention to resolve or reject it.
const other = new Promise(function(resolve, reject) {
    updatedFields.last_login_at = newDateNow();
    if (updatedFields.isActivated) {
        updatedFields.activated_at = newDateNow();
        updatedFields.deactivated_at = null;
    } else if (updatedFields.isActivated === false) {
        updatedFields.deactivated_at = newDateNow();
        updatedFields.activated_at = null;
    }
    resolve(updatedFields);
});

await Promise.all([other]);
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