JSFiddle - React, Tailwind, and code Playground
JavaScript
function getFunc(name="def_name"){
let func = function(resolve, reject)
{
let f = function()
{
let r = Math.floor(Math.random()*100)+1;
if(r!=55)
{
setTimeout( function(){ f(); }, 20 );
}
else
{
resolve("won! "+func.n);
}
}
f();
};
func.n=name;
return func;
};
class MyP extends Promise {
constructor(func)
{
super(func);
//console.log("MyP: "+MyP.count++);
//console.log("instanceof "+(this instanceof MyP));
}
success(resolve, reject)
{
return this.then((val)=>{console.log("val: "+val);}, reject);
}
}
MyP.count=0;
let a = [];
for(let i = 0; i<10; i++)
{
let p = new MyP(getFunc("p"+i));
p.name="p"+i;
a.push(p);
}
//console.log("hr");
let p = MyP.race(a);
// console.log("is p instance of MyP? "+(p instanceof MyP));
p.success();