Dear participants please tell me the solution.
In this block of code, the catсh method perfectly catches the exception:
const myPromise = new Promise(() => {
  throw new Error(`Oops! Threw an exception`);
});
// We catch the exception in the method `catch`.
myPromise
  .catch((error) => console.log(error.message));
And in this block, the catсh method will not be called:
сonst TIMEOUT = 1000;
const mySecondPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    throw new Error(`Error in asynchronous function`);
  },
  TIMEOUT
  );
});
mySecondPromise
  .catch(() => console.log(`This code will not be executed`));
Please explain:
- Why is this happening (I suppose this is due to Event Loop)?
- How to rewrite the code so that catching an exception in the catch method works with setTimeout?
Thank you all for the answers!
Here is a life example:
import moment from "moment";
const Delay = (timeout, timePress) => {
    return new Promise((res => setTimeout(() => {
            res(`${moment().format("LTS")} ${timeout} ${timePress}`);
        }, timeout * 1000)
    ));
};
export default Delay;
I want, If for some reason an exception is thrown in the setTimeout function, I should be able to catch it. Like this:
Delay.catch(() => console.log(`This code will not be executed`));
 
    