-1

I would like to be able to have 2 functions, run 1 of them, then inside it, run another and wait for its response before continuing in an if statement. The response is manual, (yes/no).

Basically something like this

function function1(){
  if(function2("Delete", "are you sure", "warning") == true){ //stop here and wait for function2 to return true or false
    console.log("yes");
  } else {
    console.log("no");
  }
}

function function2($title, $text, $type){
  swal({
    title: $title,
    text: $text,
    type: $type,
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes',
    cancelButtonText: 'No, cancel!',
    confirmButtonClass: 'btn btn-success',
    cancelButtonClass: 'btn btn-danger',
    buttonsStyling: false,
    reverseButtons: true
  }).then((result) => {
    if (result.value) {
      return true;
    } else if (
      // Read more about handling dismissals
      result.dismiss === swal.DismissReason.cancel
    ) {
      return false;
    }
  })
}

What is the cleanest and easiest way to achieve this, in Javascript or JQuery? It can't be instant, as I need the first function to pause and wait for the user response before continuing the function.

S_R
  • 1,818
  • 4
  • 27
  • 63
  • I would say the exact way you're doing it is fine. What is the problem? And since it'll both me, just `return $id < 10`' – tymeJV Feb 12 '18 at 21:44
  • You don't have to wait, it's instant. – Striped Feb 12 '18 at 21:45
  • What's there to wait for? You're going to get the response immediately. Did you test your code example before asking for help? –  Feb 12 '18 at 21:45
  • Also, _especially_ since you are returning a boolean type from `function2()`, `== true` is not necessary. – rmlan Feb 12 '18 at 21:45
  • My apologies, let me amend the question. – S_R Feb 12 '18 at 21:48
  • 2
    My guess is the code is actually asynchronous, but your sample code does not show it. – epascarello Feb 12 '18 at 21:49
  • Please provide sample implementation (short) for `function2` that demonstrates the issue (i.e. the waiting part). It doesn't have to be the actual function, but at least one we can see what is happening (is it asynchronous, does it *return* the value, or does it pass that value to a callback, or does it return a promise...) – trincot Feb 12 '18 at 21:53
  • How do you know if yes or no is clicked from the popup currently? Still not enough info in the question for us to help. – tymeJV Feb 12 '18 at 21:55
  • ^ yep. Are you using the javascript prompt or do you have some custom popup implementation? – JasonB Feb 12 '18 at 21:55
  • 99.9999999% sure it is just a dupe of https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello Feb 12 '18 at 21:56
  • @S_R *"Function 2 just simply either returns true or false."* Well problem is it is not simple depending on HOW you get that true or false. Is it an Ajax call? Is it a dialog, etc. If it was simple, I am 100% sure you would have not had to ask this. SO how do you get the data to determine the boolean state? – epascarello Feb 12 '18 at 21:57
  • don't think its a duplicate. If you're creating a popup and waiting for user input, think its safer to bind an event to the response. `if(true) { // show popup and bind event on click }` `buttonEl.addEventListener('click', function (e) { // use response }, false)` – lkroneman Feb 12 '18 at 21:59
  • @LennartKroneman someone finally added the code and I am 100% sure there is a duplicate for this since it is common. – epascarello Feb 12 '18 at 22:01
  • @S_R wohooo, you added the code and it shows you have an asynchronous method so there is no way to wait. You need to use promises or callbacks. – epascarello Feb 12 '18 at 22:03
  • @epascarello think you're right, haven't been on here long enough to recognise the common questions. – lkroneman Feb 12 '18 at 22:03
  • @S_R if `swal` is a `promise` return it from function two. `function2("Delete", "Are You Sure", "Warning").then(function1)` `function1(booleanResult) { if (booleanResult) { ... } else { ... } }` – lkroneman Feb 12 '18 at 22:06
  • @LennartKroneman I just get an error of `cannot read property then of undefined` – S_R Feb 12 '18 at 22:14
  • You must `return swal( //...etc`, and `then` will work if you do that. – trincot Feb 12 '18 at 22:18
  • @trincot apologies but can you emphasise? – S_R Feb 12 '18 at 22:22
  • You must use the `return` keyword. Insert it before `swal(`. Then your function will return a so-called promise. And promises expose a `then` method. You can then do what Ikroneman suggested above. – trincot Feb 12 '18 at 22:24
  • Brilliant, thank you, I will post the code as an answer. – S_R Feb 12 '18 at 22:29

2 Answers2

0

This might be what you looking for, not sure. function1 is waiting the promise to be resolve to console.log.

function function1(){
  function2().then(res => console.log(res ? "yes" : "no"));
}

function function2($id){
  return new Promise(resolve => resolve);
}
Striped
  • 2,544
  • 3
  • 25
  • 31
0

Answer resolved through comments of the original post.

function function2($title, $text, $type){
  return swal({
    title: $title,
    text: $text,
    type: $type,
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes',
    cancelButtonText: 'No, cancel!',
    confirmButtonClass: 'btn btn-success',
    cancelButtonClass: 'btn btn-danger',
    buttonsStyling: false,
    reverseButtons: true
  }).then((result) => {
    if (result.value) {
      return true;
    } else if (
      // Read more about handling dismissals
      result.dismiss === swal.DismissReason.cancel
    ) {
      return false;
    }
  })
}

function function1(){    
 function2("Discard", "are you sure?", "warning").then(function(response){
    if(response == true){

    } else {

    }
 });
}
S_R
  • 1,818
  • 4
  • 27
  • 63