Skip to content

Instantly share code, notes, and snippets.

@toddzebert
Last active March 8, 2017 04:23
Show Gist options
  • Save toddzebert/d41f06a7a33f5ada1557637a89278531 to your computer and use it in GitHub Desktop.
Save toddzebert/d41f06a7a33f5ada1557637a89278531 to your computer and use it in GitHub Desktop.
A simple implementation of a "race" callback pattern such that the first one of an array `entries` callbacks to either resolve or reject determines the overall result
var once = function (func) {
var done = false;
return function (res) {
if (done) return;
if (res) {
func.apply(this, arguments);
} else {
func.apply(this, false);
}
done = true;
}
}
function race (entries, cb) {
var onceCB = once(cb);
entries.map(entry => { entry(data, onceCB) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment