Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!