JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://rawgit.com/WebReflection/0ca8fac8013f76fb4d06/raw/1bdfb5f472f59dfc3ce2855fdd0592265178c26e/Promise-rejet.js"></script>
JavaScript
var simulatedCall = function(i){
return new Promise(function(res, rej, c){
var t = setTimeout(res.bind(null, i), 100 - i);
c(clearTimeout.bind(window, t));
});
}
// try to get data from multiple sources
var ps = [1,2,3,4,5,6,7].map(function(el){
return simulatedCall(el);
});
// now abort all requests as soon as the first arrives. Maybe `race` and possibly a `.some` addition should do this?
ps.forEach(function(p){
p.then(function(res){
alert("Got result! " + res);
ps.forEach(function(p){
p.cancel().catch(function(){}); // cancel others
});
});
});