什么是正确的方法(嵌套函数或...)

时间:2014-12-17 08:07:41

标签: javascript node.js asynchronous

我的示例在功能方面是否相同,考虑到错误处理我终止res.json(400,err)的事实?另外我想知道在我的第二个例子中第二个async.each总是在第一个async.each之后运行,所以在第二个async.each中使用results1是安全的吗?对不起,我是Node和async的新手!

示例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;
            }
        }
    });
}

2 个答案:

答案 0 :(得分:1)

这两个例子在设计上有所不同。第一个示例将在第一次异步成功后运行第二个异步。但是第二个例子每次都会运行第二个异步,如果有错误的话。

答案 1 :(得分:1)

第一个例子是要走的路,如果你想在第二组中使用第一组调用的结果。第二个示例不起作用,因为第二个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 */);