繁体   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