Node.js - 回调在被击中时不停止执行
Node.js - Callback don't halt the execution when got hit
我一直在使用 node.js 开发一个应用程序,在极少数情况下 回调在获取它时不会停止执行并继续执行
方法一
utilityMethod(arg1, arg2, arg2,function(err,result){
if(err){
callback(err);
}
if(result){
callback(null,result);
}
else{
callback('an error');
}
});
function utilityMethod(arg1, arg2, arg3, callback){
//some logic
if(err){
return callback(err);
}
else{
return callback(null,'success');
}
}
所以我使用下面的代码(方法 2)来停止执行
方法二
utilityMethod(arg1, arg2, arg2,function(err,result){
if(err){
return callback(err);
}
if(result){
return callback(null,result);
}
else{
return callback('an error');
}
});
我不确定为什么方法 1 会失败。哪一个是正确的使用方法?
还有为什么方法1失败了?
**注意:我在 async.each() 中使用上述方法。
**
你的方法 1 的错误是你的 if(err)
子句中没有 return 语句,所以你的回调方法被调用了两个次。
如果你真的想停止程序的执行,你应该在这里做的是抛出异常
Model.find().exec(function(err,result) {
if(err) {
throw new Error(err);
}
// .. do what you want with the result ..
});
要退出函数,您需要使用 return
。来自 documentation:
When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.
所以模型 2 是正确的方法,尽管它可以稍微清理一下:
Model.find().exec(function(err, result) {
if (err) { return callback(err); } // will exit here if error
// no need to explicitly return here as it's the termination of the function
callback(null, result); // result could be null but callback can handle that
});
除非您在此函数中执行某些操作,否则您只需使用以下代码即可将此逻辑推入 callback
:
Model.find(callback);
function callback(err, result) {
if (err) {
// do something with err
}
// do soemthing with result
// ....
}
我一直在使用 node.js 开发一个应用程序,在极少数情况下 回调在获取它时不会停止执行并继续执行
方法一
utilityMethod(arg1, arg2, arg2,function(err,result){
if(err){
callback(err);
}
if(result){
callback(null,result);
}
else{
callback('an error');
}
});
function utilityMethod(arg1, arg2, arg3, callback){
//some logic
if(err){
return callback(err);
}
else{
return callback(null,'success');
}
}
所以我使用下面的代码(方法 2)来停止执行
方法二
utilityMethod(arg1, arg2, arg2,function(err,result){
if(err){
return callback(err);
}
if(result){
return callback(null,result);
}
else{
return callback('an error');
}
});
我不确定为什么方法 1 会失败。哪一个是正确的使用方法? 还有为什么方法1失败了?
**注意:我在 async.each() 中使用上述方法。 **
你的方法 1 的错误是你的 if(err)
子句中没有 return 语句,所以你的回调方法被调用了两个次。
如果你真的想停止程序的执行,你应该在这里做的是抛出异常
Model.find().exec(function(err,result) {
if(err) {
throw new Error(err);
}
// .. do what you want with the result ..
});
要退出函数,您需要使用 return
。来自 documentation:
When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.
所以模型 2 是正确的方法,尽管它可以稍微清理一下:
Model.find().exec(function(err, result) {
if (err) { return callback(err); } // will exit here if error
// no need to explicitly return here as it's the termination of the function
callback(null, result); // result could be null but callback can handle that
});
除非您在此函数中执行某些操作,否则您只需使用以下代码即可将此逻辑推入 callback
:
Model.find(callback);
function callback(err, result) {
if (err) {
// do something with err
}
// do soemthing with result
// ....
}