I
I
Igor2019-05-03 21:24:00
AJAX
Igor, 2019-05-03 21:24:00

How to pass a parameter from a nested function?

I don’t know, maybe the question is idiotic, but I can’t find a solution, maybe I don’t know how to search. There is some wrapper for standard ajax jquery function

var flag ;

function query(data, mid, elem){
    
    $.ajax({
            type: 'POST',
            url: path_method,
            data:{ 'data': data, 'm':mid },
            beforeSend: function(){
                loading(elem, 'start');
            },
            success: function(server_response){
                var data = eval("("+server_response+")");
                loading(elem, 'stop');
                
                if(data.status){
                    status(true, data.msg)
                   flag = true;  // в случае успешного выполнения запроса
                 }else{
                    status(false, data.msg)
                  flag = false; // в случае если вернулась ошибка
                }
                
            },
            error: function(err, texterr){
                status(false, 'error_ajax'+texterr)
            }
        })
    
   return flag;  // после выполнения функции flag возвращается как undefined
}

Actually the question itself is how to pass the flag parameter to the return of the query() function, what did it return successfully to me or not? Or maybe there is another way to implement it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stockholm Syndrome, 2019-05-03
@Igor_307

read about asynchrony in JavaScript
at the moment when the request is executed and success is called, it return flag;will work for a long time,
you can pass callback to the function

function query(data, mid, elem, callback) {
  $.ajax({
    /* ... */
    success: function (server_response) {
      /* ... */
      if (data.status) {
        status(true, data.msg)
        callback(true);
      } else {
        status(false, data.msg)
        callback(false);
      }

    },
    error: function (err, texterr) {
      status(false, 'error_ajax' + texterr)
    }
  });
}


query(data, mid, elem, (flag) => {
  console.log(flag);
});

possible by promise
function query(data, mid, elem) {
  const executor = (resolve, reject) => {
    $.ajax({
      /* ... */
      success: function (server_response) {
        /* ... */
        if (data.status) {
          status(true, data.msg)
          resolve(true);
        } else {
          status(false, data.msg)
          resolve(false);
        }
  
      },
      error: function (err, texterr) {
        status(false, 'error_ajax' + texterr)
        reject();
      }
    }); 
  };
  
  return new Promise(executor);
}


query(data, mid, elem).then((flag) => {
  console.log(flag);
});

if you want to write asynchronous code synchronously, you can use promises and async/await
(async () => {
  const flag = await query(data, mid, elem); 
  console.log(flag);
})();

D
dollar, 2019-05-03
@dollar

You are making an asynchronous request.
The simplest solution is to add in the ajax options. But it is much better to understand this issue and understand what you are doing and why you need it. async: false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question