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

266
Vistas
Why is the second console log not executed?

This is the code

const setError = "";

function validate() {
  return new Promise((resolve, reject) => {
    const exp = RegExp("w+@example.com").test("sfsdf@gmail.com");
    console.log(exp);
    if (!exp) {
      setError = "Email Not Valid!";
    }
    console.log("The value is" + setError);
    if (setError != "") {
      reject();
    } else {
      resolve();
    }
  });
}

validate()
  .then(() => {
    console.log("Resolved");
  })
  .catch(() => {
    console.log("Rejected");
  });

When I run it, I get the following output,

false
Rejected

Why is the second console.log inside the validate function not executed?

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

0

setError should not be a const, when you try to change its value it throws an error and reject the Promise.

let setError = "";
about 4 years ago · Juan Pablo Isaza Denunciar

0

Because you are assigning to const setError, it causes runtime error and stop the function execution.

const cannot be re-assigned after initialization. Making it let solves the problem

let setError = "";

function validate() {
  return new Promise((resolve, reject) => {
    const exp = RegExp("w+@example.com").test("sfsdf@gmail.com");
    console.log(exp);
    if (!exp) {
      setError = "Email Not Valid!";
    }
    console.log("The value is" + setError);
    if (setError != "") {
      reject();
    } else {
      resolve();
    }
  });
}

validate()
  .then(() => {
    console.log("Resolved");
  })
  .catch(() => {
    console.log("Rejected");
  });

about 4 years ago · Juan Pablo Isaza Denunciar

0

The second console.log is not firing because setError is a constant and cannot be changed. Changing const to let should generate the output you expect:

false
The value isEmail Not Valid!
Rejected
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