0

At the end I included two example promises to check that the code works and it does work:

async function slower(arr: Promise<any> []): Promise<[number ,any] | undefined | any[]> {
    /* here we check if one is rejected in the question i solved 
       we have to return or print something if one of the two 
       promises has rejected its just an extra
    */

    let firstisresolved = false;
    let secondisresolved = false;
    try {
        await arr[0].then(function(){
            firstisresolved = true;
        });

        await arr[1].then(function(){
            secondisresolved = true;
        });
    } catch {
        console.log("something went bad");
    }

    /* we get an array of tow promises and we basically check which 
       one is faster-using promise.race()() and promise.all
    */

    if (!(secondisresolved && firstisresolved )) {
        console.log("there is one promise got rejected");
        return;
    } else {
        let num =1;
        try {
            /* here its very important to run promise.race() before 
               running promise.all() because if you run all and then 
               race the promises would be already done so you it will 
               return the first one any way so remeber to run 
               promise.race() before promise.all()
            */
            let a = await Promise.race(arr);
            let b = await Promise.all(arr);
            if(a === b[0]){ 
                num = 0;
            }
            // console.log(num);
            // console.log(a);
            let c = [num,a]
            return c;
        } catch(err) {
            console.log("there is a problem")
        }
    }
}

const promise1 = new Promise((resolve, reject) => {
    setTimeout(resolve, 5000, 'one');
});

const promise2 = new Promise((resolve, reject) => {
    setTimeout(resolve, 6000, 'two');
});

console.log(slower([promise1,promise2]));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 8
    This is all very interesting, but what is the question? – trincot Jun 11 '20 at 16:49
  • "*… and it does work*" - so what are you asking for? – Bergi Jun 11 '20 at 17:33
  • Notice that actually it doesn't work properly, see https://stackoverflow.com/questions/46889290/waiting-for-more-than-one-concurrent-await-operation – Bergi Jun 11 '20 at 17:35
  • It's probably more beneficial to write a `nSettled(iterable, n)` function largely mimicking the behaviour of `Promise.allSettled(iterable)` but with (a) each outcome object having an additional 'ordinal' property reflecting the settlement order, and (b) any promises still pending at `n` settlements represented by a `{ status:'pending', ordinal:0 }` outcome. It would be the caller's responsibility to find, amongst the array of outcomes, the one corresponding to the second (or whatever) promise to settle. eg, `let secondFastest = outcomes.find(outcome => outcome.ordinal === 2)`. – Roamer-1888 Jun 11 '20 at 22:00
  • You say that "its very important to run promise.race() before running promise.all() because if you run all and then race the promises would be already done". However, at the point where `promise.race()` appears, both promises will have settled due to `await arr[0]` and `await arr[1]` earlier in the function. – Roamer-1888 Jun 13 '20 at 01:22

0 Answers0