0

I declared a variable named check = false and I want re-assign it equal true but when it ran through the "if" condition, it was still equal false

If you know how to fix it, please help me. Thank you very much!

function deletePost(event) {
  // Get a reference to the button element
  var check = false;
  Swal.fire({
    title: "Are you sure to reject this candidate?",
    icon: "warning",
    showCancelButton: true,
    confirmButtonColor: "#3085d6",
    cancelButtonColor: "#d33",
    confirmButtonText: "Yes, i'am sure",
    customClass: {
      title: "my-title",
      content: "my-content",
      confirmButton: "my-confirm-button",
      cancelButton: "my-cancel-button",
    },
  }).then((result) => {
    if (result.isConfirmed) {
      Swal.fire(
        "Rejected Successfully!",
        "The Candidate will be informed.",
        "success"
      );
      check = true;
    }
  });
  console.log(check);
  event.preventDefault();
  if (check) {
    window.location.href = event.currentTarget.getAttribute("href");
    console.log(check);
  } else {

  }
}

I tried to re-assign the value of check equal true but I didn't work

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • Remove the `check` variable entirely and put the logic within your if-statement inside of your `.then()` callback instead of the `check = true;` line. The `.then()` method runs asynchronously, so the code after the `.then()` callback runs before the `.then()` runs and sets the `check` variable – Nick Parsons Mar 19 '23 at 10:25
  • You can also look into using [`async`/`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) to help with readability, – Nick Parsons Mar 19 '23 at 10:34

0 Answers0