Skip to content

Instantly share code, notes, and snippets.

@icholy
Created May 19, 2013 15:38
Show Gist options
  • Save icholy/5608026 to your computer and use it in GitHub Desktop.
Save icholy/5608026 to your computer and use it in GitHub Desktop.
var callbacks = [1, 2, 3].map(function (x) {
return function () {
console.log(x);
};
});
var wrong_invoke = function () {
callbacks.forEach(function (callback) {
setTimeout(callback, 0);
});
};
var correct_invoke = function () {
setTimeout(function () {
callbacks.forEach(function (callback) {
callback();
});
}, 0);
};
@rtm
Copy link

rtm commented May 19, 2013

Yes, but isn't "right" and "wrong" a little too "black" and "white"? It is certainly the case that all the major libraries seem to do it the way you call "right", but is the other way "wrong"?

@icholy
Copy link
Author

icholy commented May 19, 2013

@rtm if there's a spec, a library's behaviour can be:

1. conformant
2. non-conformant
3. undefined (by the spec)

In this case, the spec does define the correct behaviour: executing the callbacks in the order they were registered.
This leaves only two options, so I would argue that this is a black and white situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment