Ok, that's your code: http://jsfiddle.net/848AE/1/
Look in console (i change success event to failure, but it doesn't matter).
function test(param) {
console.log('------------------------------');
console.log('param = ' + param);
var tt = [ "a", "b", "c" ];
for ( var i = 0; i < param; i++) {
console.log('Before if-check, i = ' + i);
if (tt[i] == "b") {
console.log('Request start, after if-check i = ' + i);
Ext.Ajax.request( {
url : 'test.do',
method : 'POST',
failure: function(response) {
console.log('Failure start, i = ' + i);
test(1);
console.log('Alert(i) = ' + i);
}
});
// break;
}
}
}
test(3);
Log from first code (without break word in cycle):
------------------------------
param = 3
Before if-check, i = 0
Before if-check, i = 1
Request start, after if-check i = 1
Before if-check, i = 2
POST http://fiddle.jshell.net/_display/test.do 404 (NOT FOUND) ext-all.js:21
Failure start, i = 3
------------------------------
param = 1
Before if-check, i = 0
Alert(i) = 3
As you can see, when you start request, function didn't stop to execute and continue to work in cycle (i = 2, 3). So, when you get response from server, variable i has value 3.
Then, you call function with argument 1, it's finish and go back to failure function, but in failure function scope the variable i still has a value 3.
So, if i understand you right, you have to add break word in cycle as in code comment.
Edit:
If do you like to execute requests in cycle with pausing until callback executed, there is not documented property:
var response = Ext.Ajax.request({
...
async: false,
...
})
Result with async:false :
param = 3
Before if-check, i = 0
Before if-check, i = 1
Request start, after if-check i = 1
POST http://fiddle.jshell.net/_display/test.do 404 (NOT FOUND) ext-all.js:21
Failure start, i = 1
------------------------------
param = 1
Before if-check, i = 0
Alert(i) = 1
Before if-check, i = 2