How to return the value from the anonymous function in callback



  • There is ajax request for a result to be obtained from it and assigned to the variable queryResult, then the variable queryResult shall be checked and if it means something that is not the same, Partition() should be removed:

    function addPartition(){
        var queryResult;
        $.post('controllers/handlerPartition.php',
            {'isExistPartition': true, 'partitionName':   $('#modal_win1_partition_name').val()},
            function (result) {
                return result; //Как получить это значение из вне?
            }
         );
            if (queryResult === 'false') return;
    }
    

    I tried that:

    function addPartition(){
        var queryResult;
        $.post('controllers/handlerPartition.php',
            {'isExistPartition': true, 'partitionName':   $('#modal_win1_partition_name').val()},
            function (result) {
                queryResult = result; //Присвоение внешней переменой 
            }
        );
       alert(typeof queryResult); //undefined
       if (queryResult === 'false') return;
    }
    

    But the meaning of queryResult is not clear. I understand that for asynchronicity?



  • Use it. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Promise - and you need to get who you are.

    In your case, jQuery provides his version:

    var jqxhr = $.post( "example.php", function() {
      alert( "success" );
    })
    .done(function() {
      alert( "second success" );
    })
    .fail(function() {
      alert( "error" );
    })
    .always(function() {
        alert( "finished" );
    });
    

    One promise can be signed several times:

    jqxhr.always(function() {
      alert( "second finished" );
    });
    

    Also draw attention. http://reactivex.io/ of https://github.com/paldepind/flyd flows.

    Article http://blog.reactandbethankful.com/posts/2015/09/15/understanding-the-functional-revolution/ ♪


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2