-1

this is my function

function checkuploadpincode(pin_check){
    var book = new Books('books/bookupload_pin_check/'+pin_check)
    book.fetch(function (data) {
      console.log(data);
      if(data.status==true){
        alert('service available');
        return true;
      }else{
        alert('Service not available.');
        return false;
      }
    },function(data){
      alert('Service not available.');
       return false;
    });
}

$('#continue1').click(function(){
    if($('#activeSchool').hasClass('active')){
      var school_pin_check = $("input[name=school_pin_check]").val();
      checkuploadpincode(school_pin_check);

      if(isbn==''){
        alert('ISBN field Required');
        return false;
      }
    }
});

need help when i run above script it execute checkuploadpincode() first then other condition. but it execute other then my function. i thing my function takes time to execute that's why other condition execute first. i need to run my function complete then bellow condition.

dev dev
  • 121
  • 1
  • 2
  • 17

2 Answers2

2

try to use a Promise.

 function checkuploadpincode(pin_check){
        return new Promise(function(resolve){
        var book = new Books('books/bookupload_pin_check/'+pin_check)
        book.fetch(function (data) {
          console.log(data);
          if(data.status==true){
            alert('service available');
           resolve(true);
          }else{
            alert('Service not available.');
            resolve(false);
          }
        },function(data){
          alert('Service not available.');
           resolve(false);
        });
        });
    }

    $('#continue1').click(async function(){
        if($('#activeSchool').hasClass('active')){
          var school_pin_check = $("input[name=school_pin_check]").val();
          var result = checkuploadpincode(school_pin_check).then(function(res){
            if(isbn==''){
              alert('ISBN field Required');
              return false;
            }
            return true;
          })
        }
    });
0

use this

  function checkuploadpincode(pin_check){
        var book = new Books('books/bookupload_pin_check/'+pin_check)
        book.fetch(function (data) {
          console.log(data);
          if(data.status==true){
            alert('service available');
            return true;
          }else{
            alert('Service not available.');
            return false;
          }
        },function(data){
          alert('Service not available.');
           return false;
        });
    }

    $('#continue1').click(function(){
        if($('#activeSchool').hasClass('active')){
          var school_pin_check = $("input[name=school_pin_check]").val();
          checkuploadpincode(school_pin_check,function(){
              if(isbn==''){
            alert('ISBN field Required');
            return false;
          }
});
        }
    });
Jay
  • 703
  • 9
  • 21