I am relatively new to javascript and have decided to write a promise api myself. The function takes promises as a parameter and returns the last one resolved. Instead of deleting resolved promises in multiplies them in a strange way. Please help if you can.
function remove(array,index){
return array.slice(0,index).concat(array.slice(index+1));
}
Promise.last=function(arr){
let leng=arr.length;
for(let i=1;i<leng;i++){
console.log("loop");
Promise.race(arr)
.then((res)=>{
arr=remove(arr,arr.indexOf(res));
}
);
}
return arr;
}
const promise1 = new Promise((resolve,reject)=>
setTimeout(resolve, 100 , "firstpromise"));
const promise2 = new Promise((resolve,reject)=>
setTimeout(resolve, 2000 , 'secondpromise'));
const promise3 = new Promise((resolve,reject)=>
setTimeout(resolve, 3000 , 'thirdpromise'));
console.log(Promise.last([promise1,promise2,promise3,22]));
This is what it outputs in the console: [Promise, Promise, Promise,22]