I have this for loop:
for(var i = 0; i < characters.length; i++)
{
   var character = characters[i];
   charService.get(character.name).then(function (characterData)
   {
        //I want character to be captured here, i.e. the character in the current loop
        character.data = characterData;
   });
}
As charService.get() is asynchronous, by the time the callback is executed, character is the last element of the array (because the loop has ended), hence all the previous character are left alone with no data.
How can I guarantee that character.data inside the callback is referencing the character of the loop the asynchronous method was executed?
 
     
    