Strange behaviour of asynchtonous function with rejected promise

Viewed 33

Today I was trying excersice from a book, need to simulate Promise.all. My code now looks like this

async function Promise_all(promises) {
    let result = [];
    for(let promise of promises) {
       let resolvedPromise = await promise;
       result.push(resolvedPromise);
    }
    return result;
}


function soon(val) {
  return new Promise(resolve => {
    setTimeout(() => resolve(val), Math.random() * 500);
  });
}

And there one interesting thing call:

     Promise_all([Promise.reject(1), soon(2)])
.then(array => console.log('Array:', array))
.catch(error => console.log('Error:', error));

works fine, as I expected, but with call:

    Promise_all([soon(1), Promise.reject(1), soon(2)])
.then(array => console.log('Array:', array))
.catch(error => console.log('Error:', error));

I always get UnhandledPromiseRejectionWarning. Can somebody tell why this is hapenning? I also tried exception handling, but it didn't fix this problem. Thank you in advance.

0 Answers
Related