Last active
March 8, 2017 04:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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