async.auto中的任务结果

5 javascript asynchronous node.js

我对async.auto中从一个任务到另一个任务的结果逻辑感到困惑.例如,在下面的代码逻辑中,我向模型中添加了一些数据task1,这些数据最初是从模型的输出initialtaskfinalTask添加的数据task1中反映出来的results.initialTask1.同样添加的数据task2反映在results.initialTask1finalTask.

综上所有的results.initialTask1,results.task1[0],results.task2[0],results.task3[0]在相同的finalTask.这是逻辑async.auto吗?或者它是否像C++中的指针引用那样导致模型的任何变化task1,它也反映在模型中initialTask

async.auto({
    initialTask: function(callback) {
        //Do some operations
        callback(null, name, initialModels);
    },
    task1: ['initialTask', function(callback, results) {
        var models = results.initialTask[1];
        //Add some more data to models
        callback(null, models);
    }],
    task2: ['initialTask', function(callback, results) {
        var models = results.initialTask[1];
        //Add some more data to models
        callback(null, models);
    }],
    task3: ['initialTask', function(callback, results) {
        var models = results.initialTask[1];
        //Add some more data to models
        callback(null, models);
    }],
    finalTask: ['task1', 'task2', 'task3', function(callback, results) {
        //Here the followings are the same: results.initialTask[1], results.task1[0], results.task2[0], results.task3[0]                               
    }]
});
Run Code Online (Sandbox Code Playgroud)

我正在寻找任何答案,这有助于我确保这是逻辑与否?我不一定要找任何官方文件或......

wjo*_*sto 7

这是预期的行为.基本上async.auto将按照它认为必要的顺序执行所有功能.所以在你的情况下initialTask将首先调用.然后task1,task2并且task3将并行调用.最后finalTask将调用结果.所有值相同的原因与JavaScript的call-by-sharing有关,这意味着如果你自己更改一个函数参数,那么它不会影响输入参数的项目.如果更改参数的内部,它将携带到项目.

更多信息在这里.

例:

async.auto({
// this function will just be passed a callback
readData: async.apply(fs.readFile, 'data.txt', 'utf-8'),
showData: ['readData', function(results, cb) {
    // results.readData is the file's contents
    // ...
}]
}, callback);

async.auto({
get_data: function(callback) {
    console.log('in get_data');
    // async code to get some data
    callback(null, 'data', 'converted to array');
},
make_folder: function(callback) {
    console.log('in make_folder');
    // async code to create a directory to store a file in
    // this is run at the same time as getting the data
    callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(results, callback) {
    console.log('in write_file', JSON.stringify(results));
    // once there is some data and the directory exists,
    // write the data to a file in the directory
    callback(null, 'filename');
}],
email_link: ['write_file', function(results, callback) {
    console.log('in email_link', JSON.stringify(results));
    // once the file is written let's email a link to it...
    // results.write_file contains the filename returned by write_file.
    callback(null, {'file':results.write_file, 
'email':'user@example.com'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});
Run Code Online (Sandbox Code Playgroud)