Catching an exception from the setTimeout function executed inside an instance of a Promise object [duplicate]

独自空忆成欢 提交于 2020-06-21 05:37:07

问题


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:

  1. Why is this happening (I suppose this is due to Event Loop)?
  2. 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`));

回答1:


The timeout code is executed outside the promise context. What you want to do is call 'reject' with the error:

const TIMEOUT = 1000;

const mySecondPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject(new Error(`Error in asynchronous function`));
  },
  TIMEOUT
  );
});

mySecondPromise
  .catch(() => console.log(`This code WILL be executed`));



回答2:


After some discussions and comments, my task was solved by this approach:

const someFunction = timeout => new Promise(resolve => setTimeout(resolve, timeout));

someFunction(timeout).then(() => doSomething()).catch(() => console.log(`This code will not be executed`));

If an exception occurs in an asynchronous function:

someFunction(timeout).then(ErrorEvent).catch(() => console.log(`This code will not be executed`));

Such solutions are still possible:

const testF = () => { throw new Error(`Упс! Бросили исключение.`) }

const genPromise = () => new Promise((resolve, reject) => {
    setTimeout(() => {
        try {
            const res = testF()
            resolve(res)
        } catch (e) { reject(e) }
    }, 1e3)
})

t1: {
    const log = console.log.bind(console, 't1:')
    const _then = log.bind(console, 'then:')
    const _catch = log.bind(console, 'catch:')

    genPromise()
        .then(_then)
        .catch(_catch)
        .then(_then)
}

t2: {
    const log = console.log.bind(console, 't2:')
    const _then = log.bind(console, 'then:')
    const _catch = log.bind(console, 'catch:')

    void async function () {
        let pRes = null
        try {
            pRes = await genPromise()
        } catch (e) {
            _catch(e.message)
        }
        _then(pRes)
    }()
}


来源:https://stackoverflow.com/questions/61698964/catching-an-exception-from-the-settimeout-function-executed-inside-an-instance-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!