function indexArticles(callback) {
fs.readdir("posts/", function(err, files) {
async.map(files, readPost, function(err, markdown) {
async.map(markdown, parse, function(err, results) {
async.sortBy(results, function(obj, callback) {
callback(err, obj.date);
}, function(err, sorted) {
callback({"articles": sorted.reverse()});
});
});
});
});
}
मैं यह पता लगाने के लिए इस खूबसूरत बनाने के लिए कैसे करने के लिए कोशिश कर रहा हूँ उपयोग करने के लिए प्रवाह प्रवाह संरचनाओं। ऐसा लगता है जैसे मैं async.waterfall का उपयोग करता हूं, उदाहरण के लिए, जिसके परिणामस्वरूप थोड़ा और कोड होता है, जिसमें प्रत्येक चरण को किसी अज्ञात फ़ंक्शन में लपेटा जाता है। उदाहरण के लिए, यह सिर्फ पहले दो झरने के साथ नेस्टेड संस्करण की तर्ज है:पुनर्रचना नेस्टेड कॉलबैक, Node.js, async
function indexArticles(callback) {
async.waterfall([
function(callback) {
fs.readdir("posts/", function(err, files) {
callback(err, files)
})
},
function(files, callback) {
async.map(files, readPost, function(err, markdown) {
callback(err, markdown)
})
}])
}
आप यह कैसे बढ़ेगी?
यदि आंशिक रूप से बहस नहीं केवल बाएं से, तो मैं क्या कर देख सकते हैं, उदाहरण के लिए लागू करने के लिए एक तरह से, थे
function indexArticles(callback) {
async.waterfall([
async.apply(fs.readdir, "posts/"),
async.apply(async.map, __, readPost),
async.apply(async.map, __, parse),
// etc...
])
}
जबकि 'वाटरफाल' अधिक पात्रों के साथ समाप्त हो सकता है, मुझे लगता है कि यह और अधिक पठनीय तरीके से समाप्त हो जाएगा। उन सभी एनन कार्यों में सहायता के लिए 'लागू करें' भी देखें। –
क्या आप अभी भी पोस्ट किए गए झरना उदाहरण को देख सकते हैं और मुझे बता सकते हैं कि क्या मैं इसे सही कर रहा हूं? –