Javascript async.auto中的任务结果
我有点搞不懂从一项任务到另一项任务的结果逻辑。例如,在下面的代码逻辑中,我在Javascript async.auto中的任务结果,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我有点搞不懂从一项任务到另一项任务的结果逻辑。例如,在下面的代码逻辑中,我在task1中向模型添加了一些数据,这最初是initialtask的输出,在finalTask中,从task1向模型添加的数据也反映在结果中。initialTask1。类似地,在task2中添加的数据反映在结果中。initialTask1在finalTask中 总结所有结果。initialTask1,结果。task1[0],结果。task2[0],结果。task3[0]在最终任务中是相同的。这是async.auto的逻辑吗
task1
中向模型添加了一些数据,这最初是initialtask
的输出,在finalTask
中,从task1
向模型添加的数据也反映在结果中。initialTask1
。类似地,在task2
中添加的数据反映在结果中。initialTask1
在finalTask
中
总结所有结果。initialTask1
,结果。task1[0]
,结果。task2[0]
,结果。task3[0]
在最终任务中是相同的。这是async.auto
的逻辑吗?或者是C++中指针的引用,它导致了在代码> TASK1 中模型的任何变化,它也反映在初始任务< /代码>中的模型?
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]
}]
});
我在寻找任何能帮助我确定这是否符合逻辑的答案?我不一定要找任何官方文件或 这是预期的行为。基本上,async.auto
将按其认为必要的顺序执行所有功能。因此,在您的情况下,将首先调用initialTask
。然后将并行调用task1
、task2
和task3
。最后将调用带有结果的finalTask
。所有值都相同的原因与JavaScript有关,这意味着如果更改函数参数本身,则不会影响输入参数的项。如果您更改了参数的内部结构,它将带到项目
更多信息
例如:
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);
});
async.auto是async Lib提供的非常有用和强大的函数。它有3个字段
单任务
2-并发性
3-回调
在Async.auto中,除了第一个函数外,每个函数都依赖于它的父函数,如果任何函数在执行过程中出现错误,那么它们的子函数或者说。。它们下面定义的函数将不再执行,回调将发生错误,主回调将立即返回错误
1-任务:-对象
2-并发性:-用于确定可并行运行的最大任务数的可选整数。默认情况下,尽可能多。
3-回调:-返回响应
考试-
AnyService.prototype.forgetPassword = function (res, email, isMobile, callback) {
Logger.info("In AnyService service forgetPassword email...", email);
db.User.findOne({
email: email.toLowerCase(),
deleted: false
}, function (err, user) {
if (!user) {
configurationHolder.responseHandler(res, null, configurationHolder.LoginMessage.registerFirst, true, 403)
} else {
async.auto({
token: function (next, results) {
return gereratePasswordToken(next, email, user, isMobile);
},
sendMail: ['token', function (next, result) {
return SendMailService.prototype.forgetPasswordMail(next, result.token, email, user.fullName);
}]
}, function (err, result) {
if (err == null && result != null) {
configurationHolder.ResponseUtil.responseHandler(res, null, configurationHolder.LoginMessage.forgotPassword, false, 200)
} else {
callback(new Error(configurationHolder.errorMessage.oops))
}
})
}
});
}
我在寻找任何能帮助我确定这是否符合逻辑的答案?我不一定要找任何官方文件,或者…回调将是第一个参数或结果,根据文档,结果是第一个参数