我一直在玩NodeJS
最近,我發現自己被卡住了規律的問題:如何在JavaScript中處理條件回調,同時保持乾燥?
我已經運行在主運行,這取決於一些配置參數,我需要執行額外的步驟,但是這一步是異步:
if(request.config.save) {
fs.writeFile(request.config.save, decryptedData, function(err) {
// Continue the operation with a callback...
// Perform some other ops.
if(typeof callback == 'function') callback(decryptedData);
}.bind(this));
} else {
// Continue the same operation without a callback
// Perform some other ops.
if(typeof callback == 'function') callback(decryptedData);
正如你可以看到這個代碼是不會幹爲主體的結尾(回調)被調用兩次。
只有我看到的方式是使用功能(但再次函數調用未乾......和代碼可能是非常臃腫這樣...
那麼,有一個漂亮的忍者技巧來解決這個?