Javascript 捕获在Promise对象实例内执行的setTimeout函数中的异常

Javascript 捕获在Promise对象实例内执行的setTimeout函数中的异常,javascript,promise,settimeout,Javascript,Promise,Settimeout,亲爱的参与者,请告诉我解决方案 在这段代码中,catСh方法完美地捕获了异常: 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)); 在此块中,

亲爱的参与者,请告诉我解决方案

在这段代码中,catСh方法完美地捕获了异常:

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));
在此块中,将不调用catСh方法:

с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`));
请解释:

为什么会发生这种情况?我想这是由于事件循环造成的? 如何重写代码,以便在catch方法中捕获异常与setTimeout一起工作? 谢谢大家的回答

下面是一个活生生的例子:

import moment from "moment";

const Delay = (timeout, timePress) => {
    return new Promise((res => setTimeout(() => {
            res(`${moment().format("LTS")} ${timeout} ${timePress}`);
        }, timeout * 1000)
    ));
};

export default Delay;
我想,如果出于某种原因在setTimeout函数中抛出异常,我应该能够捕获它。像这样:

Delay.catch(() => console.log(`This code will not be executed`));

超时代码在承诺上下文之外执行。您要做的是调用“拒绝”,并显示以下错误:

常数超时=1000; const mySecondPromise=新的Promiser解决方案,拒绝=>{ 设置超时=>{ 拒绝新错误“异步函数中的错误”; }, 超时 ; }; mySecondPromise
.catch=>console.log`将执行此代码' 超时代码在承诺上下文之外执行。您要做的是调用“拒绝”,并显示以下错误:

常数超时=1000; const mySecondPromise=新的Promiser解决方案,拒绝=>{ 设置超时=>{ 拒绝新错误“异步函数中的错误”; }, 超时 ; }; mySecondPromise
.catch=>console.log`将执行此代码' 经过一些讨论和评论,我的任务通过以下方法得以解决:

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

someFunction(timeout).then(() => doSomething()).catch(() => console.log(`This code will not be executed`));
如果异步函数中发生异常:

someFunction(timeout).then(ErrorEvent).catch(() => console.log(`This code will not be executed`));
这些解决方案仍然可行:

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)
    }()
}

经过一些讨论和评论,我的任务通过以下方法得以解决:

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

someFunction(timeout).then(() => doSomething()).catch(() => console.log(`This code will not be executed`));
如果异步函数中发生异常:

someFunction(timeout).then(ErrorEvent).catch(() => console.log(`This code will not be executed`));
这些解决方案仍然可行:

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)
    }()
}

你能展示你的真实代码的其余部分吗?这样我们就可以看到你真正想要做的事情了?在摘要中回答这个问题并不能让我们帮助您为实际问题提供最佳设计以及使用承诺的最佳方式。我用一个真实的例子补充了我的问题如果您想要拒绝延迟承诺,您可以声明新PromiserSolve的第二个参数,reject=>{/*code here*/}executor函数,您只需调用reject,就像您希望解决承诺一样。如果您确实想在setTimeout中使用异常,那么您需要在回调中使用try/catch,如果有人抛出,它将调用reject。我理解您的意思。非常感谢!这样的决定怎么样?Declare const Delay=timeout=>newpromiseresolve=>setTimeoutresolve,timeout;然后进行调用延迟。然后`${moment.formatLTS}${timeout}${timePress}`。catch=>console.log`将不执行此代码';很抱歉,但是延迟。那么`${moment.formatLTS}${timeout}${timePress}`对我来说没有任何意义。首先,Delay是一个函数,所以需要用Delay调用它。然后,您将函数引用传递给。然后,不是字符串。您能否显示其余的真实代码,以便我们了解您真正想要做什么?在摘要中回答这个问题并不能让我们帮助您为实际问题提供最佳设计以及使用承诺的最佳方式。我用一个真实的例子补充了我的问题如果您想要拒绝延迟承诺,您可以声明新PromiserSolve的第二个参数,reject=>{/*code here*/}executor函数,您只需调用reject,就像您希望解决承诺一样。如果您确实想在setTimeout中使用异常,那么您需要在回调中使用try/catch,如果有人抛出,它将调用reject。我理解您的意思。非常感谢!这样的决定怎么样?Declare const Delay=timeout=>newpromiseresolve=>setTimeoutresolve,timeout;然后进行调用延迟。然后`${moment.formatLTS}${timeout}${timePress}`。catch=>console.log`将不执行此代码';很抱歉,但是延迟。那么`${moment.formatLTS}${timeout}${timePress}`对我来说没有任何意义。首先,Delay是一个函数,所以需要用Delay调用它。然后,将函数引用传递给。然后,不是字符串。