Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Forked from unscriptable/ambiguous-race.js
Created October 6, 2014 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briancavalier/ad2b84950fda92bdb546 to your computer and use it in GitHub Desktop.
Save briancavalier/ad2b84950fda92bdb546 to your computer and use it in GitHub Desktop.
function logWinner (p1, p2) {
Promise.race([p1, p2]).then(console.log.bind(console));
}
var p1 = new Promise(function(resolve) {
setTimeout(function() { resolve('p1'); }, 20);
});
var p2 = new Promise(function(resolve) {
setTimeout(function() { resolve('p2'); }, 10);
});
setTimeout(function() { logWinner(p1, p2); }, 50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment