Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Created March 6, 2017 19:40
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 gskachkov/db7c2b95bb321c6d353d26a808a8d71d to your computer and use it in GitHub Desktop.
Save gskachkov/db7c2b95bb321c6d353d26a808a8d71d to your computer and use it in GitHub Desktop.
Example of usage Promise.race
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
Copy link

ghost commented Jan 18, 2018

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.

@Iiridayn
Copy link

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