Something strange is happening with my code in Chrome, creating a mysterious quantum duel state where functions turn into undefined
when pushed to an array... eerie!
I am constructing an array of functions using the code below to chain them together for callbacks: https://gist.github.com/3609831
console.log "creating stack"
ids = (id for id of @s3) # an array of integers
stack = [callback]
console.log stack
for ssid of @s3
new_func = ((cb) => @idb.load_s3(ssids.shift(),cb))
console.log new_func
stack.push new_func
console.log stack
console.log "stack done"
Although it appears to be functioning correctly - all function calls with the correct arguments are being executed, there's also this odd output in the console:
> creating stack
# callback added to array correctly
> [function () { return _this.start(true); }]
# function generated correctly
> function (cb) { return _this.idb.load_s3(ssids.shift(), cb); }
# but the function doesn't get added to the array !!!!!!!
> [function () { return _this.start(true); }, undefined × 1]
> stack done
# and the undefined from the array can't be executed of course, verified my line number
> Uncaught TypeError: undefined is not a function
This indicates that although it worked, it also didn’t work because new_func becomes undefined once pushed to the array...
Strange right?
Anyone have any insights or clues on what might be causing this issue?
I'm using chrome v21
...
EDIT: Just to clarify, the @idb.load_s3 function IS in scope and functioning properly as it loads data from indexedDB and the callback does trigger.
...
EDIT2: Here is the javascript version which looks less readable but essentially mirrors the original code:
var id, ids, new_func, ssid, stack,
_this = this;
console.log("creating stack");
ids = (function() {
var _results;
_results = [];
for (id in this.s3) {
_results.push(id);
}
return _results;
}).call(this);
stack = [callback];
console.log(stack);
for (ssid in this.s3) {
new_func = (function(cb) {
return _this.idb.load_s3(ssids.shift(), cb);
});
console.log(new_func);
stack.push(new_func);
console.log(stack);
}
console.log("stack done");
...
EDIT 3: It seems like part of the issue may stem from Chrome's asynchronous console behavior. The last item in the stack gets popped off after the console.log but before it actually displays the array contents. As long as I don't execute the function to run the stack, everything seems to function properly.
However, this still doesn't explain the exception related to undefined not being a function, making it all the more peculiar!
...
EDIT #4: load_s3 function can be found here: https://github.com/gnatters/neuroanatomist/blob/master/app/assets/jax/models/asset_loader.js.coffee.erb#L118 The actual code in question is in the same file under load_everything at #L145