在promise中的回调中抛出
Throw inside a callback inside a promise
我知道stackoverflow充满了类似的问题,我已经读了很多。
从我得到的throw
内的承诺应该拒绝它,因为我可以在文档中阅读:
如果执行器抛出异常,其值将传递给拒绝解析函数。
但是即使在阅读了很多关于promise和throw的帖子之后,我仍然不理解我粘贴的代码片段以及为什么会发生。
function foo(a, b, cb) {
setTimeout(() => {
cb('Inner error *!?"$%&#@"');
}, 0);
}
const getThePromise = () => {
return new Promise((resolve, reject) => {
const cb = (err) => {
/* >>> ************ */
throw err; // catch not called
// reject(err); // catch called
/* ************ <<< */
}
foo('foo', 'dudee', cb);
});
}
getThePromise()
.catch((err) => {
console.log('CATCH:', err);
})
.then((res) => {
console.log('then...');
})
我不明白为什么如果我使用throw
,承诺的.catch
不被调用,但如果我使用reject
,它被调用。
只是为了澄清,我在Mac OS/X 10.11中使用Node.js v6.2.2,但我不认为这也可能是浏览器问题
在异步setTimeout
调用中抛出错误,这将导致未捕获的错误。异步代码不会在与try-catch块相同的上下文中执行。这与promise API无关。这只是JavaScript中异步代码执行行为的一部分。
看一下下面的例子。
const asyncOperation = err => {
try {
setTimeout(function() {
throw err; // will be dropped onto the event queue
// until the call stack is empty
// even if this takes longer than
// a second.
}, 1000);
} catch (e) {
console.log(e) // will not be called
}
}
asyncOperation('Inner error *!?"$%&#@"')
现在是同样的例子,在setTimeout
调用内的try-catch块和在try块内抛出的错误。
const asyncOperation = err => {
setTimeout(function() {
try {
throw err // here the error will be throw inside
} catch (e) { // the try block and has the same execution
console.log(e) // context.
}
}, 1000);
}
asyncOperation('Inner error *!?"$%&#@"')
你可以在这里找到更多关于Promise.catch
的信息。
Promise.prototype.catch ()
catch()方法返回一个Promise并且只处理被拒绝的情况。
实际上有一个与你在例子中描述的情况相同的例子。查看
抛出错误时的陷阱
// Errors thrown inside asynchronous functions will act like uncaught errors
var p2 = new Promise(function(resolve, reject) {
setTimeout(function() {
throw 'Uncaught Exception!';
}, 1000);
});
p2.catch(function(e) {
console.log(e); // This is never called
});
相关文章:
- 测试Angular Service解决错误回调中的promise
- Angularjs使用“;这个“;promise内的关键字回调
- 在promise中为回调添加超时并拒绝
- 使用回调hell将构建转换为promise hell
- 将行为与Promise回调混淆
- 与 promise 一起使用时,异步瀑布不执行下一个回调方法
- 如何在原生 JavaScript Promise 中包装 jsonP 回调
- Promise.每个在蓝鸟都有某种所有操作完成的回调吗?
- 在nodejs中将回调转换为promise
- 传递promise回调方法的更好方法
- 如何通过一个回调异步排队和执行多个promise
- Angular promise回调不是在构造函数方法内部触发,而是在对象文本方法中触发
- 如何在nodejs中使用回调或promise
- 如何在Promise回调中访问实例变量
- 绑定promise回调函数的“this”范围
- 为什么不是't Promise中延迟运行的回调
- 通过带有err,value签名的回调创建promise
- 在 Node.js 中用 promise 替换回调
- 使用 promise 的 JavaScript 回调
- 如何使用promise来避免回调地狱