2017-02-16 82 views
1

我想调用可能会遇到错误的util函数。如果发生错误,应该终止process函数。正确抛出错误的唯一方法是callback函数。node.js - 调用回调是否终止当前函数

我会通过返回来终止函数,但由于我在util函数中,所以函数将在util()之后继续。

function util(callback) { 

    // do something synchronous 

    if (err) { 
     // doesn't terminate the process function 
     // since we are in the util function 
     return callback("something unexpected happened"); 
    } 
} 

function process(callback) { 
    util(callback); 
    console.log("This should not be printed if an error occurs!"); 
} 

process(function (err) { 
    if (err) { 
     // this has to be executed in case of an error 
     console.log(err); 

     // terminate the process function somehow? 
    } 
}); 
+0

我假定你的意思** A **同步在'util',否则你不会使用回调? – Bergi

+0

@Bergi我使用回调,因为它传递给不受我控制的代码进行处理。 –

+0

我想只有'process'应该调用回调函数,'util'应该是一个标准的同步函数。 – Bergi

回答

-1

我建议你对代码进行一些修改

function util(callback) { 

    // do something synchronous 

    if (err) { 
     // doesn't terminate the process function 
     // since we are in the util function 
     callback("something unexpected happened"); 
     return false; 
    } 

    return true; 
} 

function process(callback) { 
    if(!util(callback)) return; 
    console.log("This should not be printed if an error occurs!"); 
} 
+0

无论发生什么情况,您的console.log仍将打印。或者说,无论在Util中发生什么,它都可能打印或不打印。 – EvSunWoodard

+0

由于我返回false,它不会打印,如果该函数有错误。你可以试试看。 –

+0

你试图在超时函数内返回true/false,所以返回的时间不是你原来的函数,请检查https://jsfiddle.net/r37okyt9/1/ –

0

不,你会想要做这样的事情:

  function util(callback) { 
       // do something synchronous 
       if (err) { 
        throw new Error('Something unexpected happened'); 
       } 
       callback(); // only execute callback if an error did not occur 
      } 

      function process(callback) { 
       try{ 
        util(callback); 
        console.log("This should not be printed if an error occurs!"); 
       } catch(error){ 
        process(error); 
       } 
      } 

      process(function (err) { 
       if (err) { 
        // this has to be executed in case of an error 
        console.log(err); 
        process.exit(1) 
       } 
      }); 
0

这似乎是一个很好用的时间承诺,承诺是JavaScript的强制同步功能的方式。基本上你设置它像这样:

var util = new Promise(function(resolve, reject) { 
    /* do some stuff here */ 

    if (someCondition) { 
     resolve("With a message"); 
    } else { 
     reject("with a message"); 
    } 
} 

function process() { 
    util.then(function() { 
     console.log("Won't call if there is an error"); 
    }).catch(function() { 
     console.log("There was an error, Bob"); 
    }); 
} 
+0

你必须传递函数到'then' – Bergi

+0

你还没有传递一个函数,而是'undefined'。如果你想命名这些功能,请使用'onFulfill'和'onReject'。 – Bergi

+1

@Bergi - 善良我累了。现在编辑正确。 – EvSunWoodard

3

是否调用回调终止当前功能?

不,回调只是一个常规功能。它当然可能会抛出异常(尽管这是鄙视)。

我想调用一个可能遇到错误的util函数。如果发生错误,则应终止过程功能。

为此,您需要在回调中检查发生了什么并据此采取行动。您可以使用process.exit进行终止。

function myProcess(callback) { 
    util(function(err, result) { 
     if (err) { 
      callback(err); 
     } else { 
      console.log("This should not be printed if an error occurs!"); 
      callback(null, result); 
     } 
    }); 
} 

myProcess(function (err) { 
    if (err) { 
     // this has to be executed in case of an error 
     console.log(err); 
     process.exit(1); 
    } 
}); 

请注意,承诺可以简化这一点,因为它们区分成功和错误回调。 util必须返回一个承诺:

function util() { 
    return new Promise(function(resolve, reject) { 
     // do something asynchronous 

     if (err) 
      reject("something unexpected happened"); 
     else 
      resolve(…); 
    }); 
} 

function myProcess() { 
    return util().then(function(res) { 
     console.log("This should not be printed if an error occurs!"); 
     return res; 
    }); 
} 

myProcess().catch(function (err) { 
    // this has to be executed in case of an error 
    console.log(err); 
    process.exit(1); // you might not even need this: 
    throw err; // The node process will by default exit with an unhandled rejection 
}); 
+0

process.exit()在我的情况下没有帮助,因为应用程序运行一个web服务器并且应该保持运行。 –

+0

@MaxMustermann Ooops,我用“*终止进程*”弄糊涂了“*终止进程函数*”。如果你只是不想做其他事情,可以使用'return'(可能在调用回调函数表示完成后,如果你的函数是异步的)。 – Bergi