1

I'm not so much pro in javascript variable scopes and got stuck with one question.

If i have function which dose ajax call and then call my callback

function doAjaxFunc(param, callback) 
{
  $.ajax({
    type: 'GET',
    url: '/some/url/'+param,
    success: function(data){        
        callback(data);
    },
    dataType:'json'
  });
}

function someCallback1(ajaxResp){
   // DO someting 1
}

function someCallback2(ajaxResp){
   // DO someting 2
}

// exec
doAjaxFunc(1, someCallback1);
doAjaxFunc(2, someCallback2);

As ajax is async and it can be that sever will process param=1 case longer then param=2 is it possible that someCallback1 and someCallback2 will process not their responses. I mean callback argument value will be somehow mixed ?

If possible give some explanation details in answer

2
  • It depends how you define "mixed". Each will get called for correct param but execution order is not guaranteed as the requests can complete in any order
    – charlietfl
    Commented Mar 16, 2021 at 12:10
  • @charlietfl under mix i mend value mixing, order is not important
    – Armen
    Commented Mar 16, 2021 at 12:13

2 Answers 2

1

I mean callback argument value will be somehow mixed?

No. The callbacks will be called in completely separate invocations within scope of the originating AJAX success handler. There will be no cross-contamination of the data from either request.

Also, just as an aside, you can change this:

success: function(data){        
    callback(data);
},

To just this:

success: callback,
4
  • Thank you for answer. So you mean that inside each ajax success function will be available only its parent doAjaxFunc function passed params right ? evan if i pass 10 diferent params to doAjaxFunc(param1,param2 ...)
    – Armen
    Commented Mar 16, 2021 at 12:17
  • Is there some name for this ? so that i will google and read more to understand reason
    – Armen
    Commented Mar 16, 2021 at 12:22
  • The general term is 'scope'. It's to do with the context within variables and parameters are accessible. You seem to already know this from your question title, though :) Commented Mar 16, 2021 at 12:24
  • ok thanks, accepting your answer as you posted first in answers list
    – Armen
    Commented Mar 16, 2021 at 12:25
0

Check this example , i hope it is some helpful to understand scope in JavaScript

var isFirstCall=false;
function doAjax(param)
{
  if(!isFirstCall)
  {
    //for example after do ajax
    var millisecondsToWait = 1000;
    setTimeout(function() {
    console.log(param);  
    }, millisecondsToWait);
  }
  isFirstCall=true;
  console.log(param);  
}

doAjax('first call');
doAjax('second call');

1
  • Thank you for answer. I understand what happening here but i would like to understand reason. So and setTimeoput and jquery ajax somehow run in exact variable scope environment and once their callback triggered only correct variable scopes are available ? how its called ?
    – Armen
    Commented Mar 16, 2021 at 12:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.