Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 暂停并等待其他函数的布尔响应_Javascript_Jquery_Sweetalert2 - Fatal编程技术网

Javascript 暂停并等待其他函数的布尔响应

Javascript 暂停并等待其他函数的布尔响应,javascript,jquery,sweetalert2,Javascript,Jquery,Sweetalert2,我希望能够有两个函数,运行其中一个,然后在其中运行另一个,等待它的响应,然后在if语句中继续。响应为手动(是/否) 基本上是这样的 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 { co

我希望能够有两个函数,运行其中一个,然后在其中运行另一个,等待它的响应,然后在
if
语句中继续。响应为手动(是/否)

基本上是这样的

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;
    }
  })
}

在Javascript或JQuery中,实现这一点最干净、最简单的方法是什么?它不可能是即时的,因为我需要第一个函数暂停并等待用户响应,然后再继续该函数。

这可能就是您要查找的,不确定。
function1
正在等待将承诺解析到
console.log

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

function function2($id){
  return new Promise(resolve => resolve);
}

答案通过原帖子的评论解决

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 {

    }
 });
}

我想说你做这件事的确切方式很好。有什么问题?既然这两个都是我的,只需
返回$id<10
“你不必等待,它是即时的。还有什么需要等待的呢?你会马上得到答复的。在请求帮助之前,您是否测试了您的代码示例?另外,特别是由于您从
function2()
返回布尔类型,
==true
是不必要的。我猜代码实际上是异步的,但您的示例代码没有显示它。这只是一个猜测。如果没有,我将删除它。