2017-02-16 30 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