Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda