Created
March 6, 2017 19:40
-
-
Save gskachkov/db7c2b95bb321c6d353d26a808a8d71d to your computer and use it in GitHub Desktop.
Example of usage Promise.race
This file contains 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 getPromise = (value, delay) => { | |
return new Promise(resolve => { | |
setTimeout(() => resolve(value), delay); | |
}); | |
}; | |
var promise1 = getPromise('value-1', 350); | |
var promise2 = getPromise('value-2', 250); | |
var promise3 = getPromise('value-3', 150); | |
var promise = Promise.race([promise1, promise2, promise3]); | |
promise | |
.then(result => { | |
console.log('result of promise:', result); | |
}) | |
.catch(error => { | |
console.log('error', error); | |
}); | |
// Will be printed 'result of promise: value-3' after small delay |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am not sure that it’s a real usage example. I suppose that race could be applied to show any deffered interaction on the UI or make request calls with some back-up functionality. For instance, we have the multiple API requests to some health/check endpoints and want return first-done value.