簡體   English   中英

什么是正確的方法(嵌套函數或…)

[英]What is the right way (nested functions or…)

考慮到在錯誤處理中我將通過res.json(400,err)終止,我的兩個示例在功能方面是否都相同? 我也想知道在我的第二個示例中,第二個async.each總是在第一個async.each之后運行,因此在第二個async.each中使用results1是安全的嗎? 抱歉,我是Node和異步的新手!

例1:我將最后一個塊中每個async.each的結果用作另一個async.each的輸入:

var results1 = {};
var results2 = {};
async.each(inputs, function (input, callback) {
    //Do something here and add some data to results1
    callback();
}, function (err) {
    if (err) {
        //Handeling error
    } else {
        async.each(results1, function (item, callback) {
            //Do something here and add some data to results2                      
        }, function (err) {
            if (err) {
                //Handeling error
            } else {
                console.log("Final result", results2);
            }

        });
    }
});

Example2:我有單獨的async.each塊

var results1 = {};
async.each(inputs, function (input, callback) {
    //Do something here and add some data to results1
    callback();
}, function (err) {
    if (err) {
        //Handeling error
    }
});


var results2 = {};
async.each(results1, function (item, callback) {
    //Do something here and add some data to results2     
    callback();
}, function (err) {
    if (err) {
        //Handeling error
    } else {
        console.log("Final result", results2);
    }
});

更新:由於第二種方法不正確,並且不能保證第二個async.each在第一個之后運行,問題是:這是否意味着我也無法像下面的示例那樣使用簡單的for循環? 如果是的話,很容易將其更改為async.each,但是問題是這是遞歸的,這使其變得復雜! 如果這也需要異步而不是for循環,那么您知道我如何在這里擁有此遞歸功能嗎?

var results1 = {};
var results2 = [];
var results3 = {};
async.each(inputs, function (input, callback) {
    //Do something here and add some data to results1
    callback();
}, function (err) {
    if (err) {
        //Handeling error
    } else {

        // So in this case that I need to have nested function, does it mean I cannot have a simple for loop like this as well?
        //  If yes, it is easy to change this one to async.each, but the problem is this one is recursive and that's make it complicated! If this needs to be async as well, do you know how I can have this recursive functionality here?
        for (var input in inputs) {
            inferFromUnion(inputs[input], results1);
            results2.push(inputs[input]);
        }

        async.each(results2, function (item, callback) {
            //Do something here and add some data to results2                      
        }, function (err) {
            if (err) {
                //Handeling error
            } else {
                console.log("Final result", results3);
            }

        });
    }
});


// Here just checking each object schema and if they are missing any fields from results1 we add that field with a value of null
function inferFromUnion(obj, allFields) {
    Object.keys(allFields).forEach(function (key) {
        if (lodash.isUndefined(obj[key])) {
            if (lodash.isPlainObject(allFields[key])) {
                obj[key] = {};
                inferFromUnion(obj[key], allFields[key]);
            } else {
                obj[key] = null;
            }
        }
    });
}

這兩個示例在設計上有所不同。 第一個示例將在第一個異步成功后運行第二個異步。 但是,第二個示例每次都會運行第二次異步,無論是否存在錯誤。

如果要使用第二個呼叫集中的第一組呼叫的結果,第一個示例就是處理方法。 第二個示例不起作用,因為第二個async.each()保證綁定到異步操作的回調之前運行。


具有循環的異步遞歸非常有可能:

(function doSomeAsyncRecursion (results) {
    async.each(someItems, function (item, callback) {
        // ...
    }, function () {
        if (results /* ... (are incomplete) */) {
            doSomeAsyncRecursion(results);
        } else {
            // ... (results are complete now, do something with them)
        }
    });
})(/* initial value of results */);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM