I have the promise in function2 which is rejected and added into the array of promises in function1. The code ends with UnhandledPromiseRejection. Can you please help me to understand why the promise is not successfully returned from function1 and caught in test function?
const sleep = require("sleep-promise");
function function1() {
const promises = [];
promises.push(function2());
return sleep(1000).then(() => {
return Promise.all(promises)
});
}
function function2() {
return new Promise((resolve, reject) => {
reject("failed");
})
}
async function test() {
try {
await function1();
} catch (e) {
console.log(e);
}
}
test();
output:
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "failed".]