I'm at a loss right now. I'm using a simple variable, whose value is assigned during a loop. After exiting the loop, the value of the variable is still undefined, unless I alert it's value first. Then everything works fine. What's going on here?
$(myarray).each(function(idx, item)
{
fetchSomethingRemotely( success: function(data) {
item.someValue = data; });
// if the following alert is not there, doSomething will never get called
// and the alert after the else will show item.someValue as undefined.
alert(item.someValue);
if (item.someValue != null) { doSomething(item.someValue); }
else { alert(item.someValue); }
});
Edit:
Okay, so I've got a better handle in this now. The value assignment (item.someValue=123) happens inside of a callback function within this iteration. So the value is probably not there yet when I serially try to access it a couple of code lines below. How could I wait for the value to be assigned?