Skip to content

Instantly share code, notes, and snippets.

@metatoaster
Last active February 16, 2017 23:15
Show Gist options
  • Select an option

  • Save metatoaster/799dedc1982c84a263003cf7f5d405de to your computer and use it in GitHub Desktop.

Select an option

Save metatoaster/799dedc1982c84a263003cf7f5d405de to your computer and use it in GitHub Desktop.
Not immediately wrong looking for loop
// not very obviously wrong looking function
var for_test_func = function(outer, inner, obj) {
for (var i in obj) {
outer(i, function() {
inner(obj[i]);
});
}
};
// problem will be more obvious when the two different implementations
// are seen, but this isn't necessarily known to the author of the
// above function.
var immediate = function(obj) {
var items = [];
var inner = function(o) {
items.push(o);
};
var outer = function(i, f) {
console.log(i);
f();
};
for_test_func(outer, inner, obj);
console.log(items);
};
var cached = function(obj) {
var callables = [];
var items = [];
var inner = function(o) {
items.push(o);
};
var outer = function(i, f) {
console.log(i);
callables.push(f);
};
for_test_func(outer, inner, obj);
callables.forEach(function(f) { f(); });
console.log(items);
};
// check the console outputs
immediate({a: 1, b: 2, c: 3, d: 4});
cached({a: 1, b: 2, c: 3, d: 4});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment