I wonder how async call works when looping through html elements with jquery. I know that async call doesn't stop iterating when server call is being made. That means, script is immediately iterating over next element in sequence, even if response from call is not yet finished.
My problem is, that on each call I have different values set, like this:
$(".businessUnit").each(function (index) {
var $element = $(this);
var iUCBU_KEY = $element.prop('data-iucbu_key');
var iENTE_KEY = $element.prop("data-iente_key");
$.ajax({
method: 'post',
url: '/CodeBase/LoadInsertCATETE_BASE1/',
data: {
iUCBU_KEY: iUCBU_KEY,
iENTE_KEY: iENTE_KEY
},
success: function (response) {
//will there be $element from sequence when call was made?
$element.find('input.price').val(response.price);
},
error: function (response) {
}
});
});
Which $element will be updated? Will it be $element from which call was made or $element in which iteration is currently? Response from server can take a few seconds, so iteration can go several elements further in between call - response.