为什么我不能Error在catch回调中抛出一个内部并让进程处理错误,就好像它在任何其他范围内一样?
如果我什么都不做console.log(err)就会被打印出去,我对发生的事情一无所知.这个过程刚刚结束......
例:
function do1() {
return new Promise(function(resolve, reject) {
throw new Error('do1');
setTimeout(resolve, 1000)
});
}
function do2() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error('do2'));
}, 1000)
});
}
do1().then(do2).catch(function(err) {
//console.log(err.stack); // This is the only way to see the stack
throw err; // This does nothing
});
Run Code Online (Sandbox Code Playgroud)
如果回调在主线程中执行,为什么Error会被黑洞吞噬?
作为节点程序员.我习惯使用"nodebacks"来处理我的代码中的错误:
myFn(param, function(err, data) {
if (err){
//error handling logic
}
else {
// business logic
}
});
Run Code Online (Sandbox Code Playgroud)
在编写该函数时,我可以执行以下操作:
var myFn = function(param, callback){
var calc = doSomeCalculation(param);
if(calc === null) { // or some other way to detect error
callback(new Error("error with calculation"), null);
}
...
someAsyncOp(calcN,function(err, finalResult){
if(err) return callback(err, null);
callback(null, finalResult); // the error is null to signal no error
});
};
Run Code Online (Sandbox Code Playgroud)
我如何使用promises进行这种错误处理?
短篇小说:
谈到Promises/A +,拒绝承诺的正确方法是什么 - 抛出错误?但如果我错过了catch- 我的整个应用程序将会爆炸!
如何使用promisify它有什么好处(也许你需要阅读更长的版本)?
是.then(success, fail)真正的反模式,我应该使用.then(success).catch(error)?
更长的版本,被描述为现实生活中的问题(希望有人阅读):
我有一个使用Bluebird(A + Promise实现库)的库,用于从数据库中获取数据(谈论Sequelize).每个查询都返回一个结果,但有时候它是空的(试图选择一些东西,但没有任何查询).承诺进入result函数,因为没有错误的原因(没有结果不是错误).例:
Entity.find(1).then(function(result) {
// always ending here, despite result is {}
}).catch(function(error) {
// never ends here
});
Run Code Online (Sandbox Code Playgroud)
我想包装它并检查结果是否为空(在我的代码中无法检查到这一点).我这样做了:
function findFirst() {
return Entity.find(1).then(function(result) {
if (result) { // add proper checks if needed
return result; // will invoke the success function
} else {
// I WANT …Run Code Online (Sandbox Code Playgroud) 我想用babel运行这段代码:
redisClientAsync.delAsync('key');
return await someOtherAsyncFunction();
Run Code Online (Sandbox Code Playgroud)
在没有等待第一行的异步函数内部.这个可以吗?
我怎么能运行一些我不在乎的东西?
我可以在没有回调的情况下触发非promisified函数del('key',null)吗?
他们已经对这个话题提出了疑问
这是旧的,答案已经过时,domains甚至已经被弃用了.
现在在Async/Await Node.js之后的场景中,我们不应该同样考虑同步和异步情况,并在同步函数中抛出异常并拒绝异步函数中的promise,而不是Error在前一种情况下返回实例.
let divideSync = function(x,y) {
// if error condition?
if ( y === 0 ) {
// "throw" the error
throw new Error("Can't divide by zero exception")
}
else {
// no error occured, continue on
return x/y
}
}
Run Code Online (Sandbox Code Playgroud)
模拟异步除法运算
let divideAsync = function(x, y) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
// if error condition?
if (y === 0) {
// "throw" the error safely by rejecting the …Run Code Online (Sandbox Code Playgroud) 这两个中间件功能表现不同,我无法弄清楚原因:
在这里,错误将被try/catch困住:
router.get('/force_async_error/0', async function (req, res, next) {
try{
await Promise.reject(new Error('my zoom 0'));
}
catch(err){
next(err);
}
});
Run Code Online (Sandbox Code Playgroud)
但是在这里,错误不会被try/catch困住:
router.get('/force_async_error/1', async function (req, res, next) {
await Promise.reject(new Error('my zoom 1'));
});
Run Code Online (Sandbox Code Playgroud)
我认为Express用try/catch包装了所有中间件函数,所以我看不出它会有什么不同的行为?
我查看了Express源代码,处理程序看起来像:
Layer.prototype.handle_request = function handle(req, res, next) {
var fn = this.handle;
if (fn.length > 3) {
// not a standard request handler
return next();
}
try {
fn(req, res, next); // shouldn't this trap the async/await error?
} catch (err) {
next(err); …Run Code Online (Sandbox Code Playgroud) javascript ×5
node.js ×4
promise ×4
async-await ×3
es6-promise ×3
bluebird ×2
asynchronous ×1
ecmascript-7 ×1
express ×1
express-4 ×1
throw ×1