Task results in async.auto

I am a bit confused by the logic of the results that go from one task to another task in async.auto . For example, in the following logic of the code, I added some data to the models in task1 , which are initially derived from initialtask and in finalTask added data to the models from task1 also reflected in results.initialTask1 . Similarly, the added data in task2 reflected in results.initialTask1 in finalTask .

To summarize , all results.initialTask1 , results.task1[0] , results.task2[0] , results.task3[0] identical in finalTask . Is this the async.auto logic? Or is it something like a pointer to a C ++ pointer that causes any changes for models in task1 , is it also reflected in models in 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] }] }); 

I am looking for any answer that helps me make sure that this is logic or not? I'm not necessarily looking for any white papers or ...

+5
source share
2 answers

This is the expected behavior. Basically, async.auto will perform all the functions in the order that it considers necessary. Therefore, in your case, initialTask will be called first. Then task1 , task2 and task3 will be called in parallel. Finally, finalTask will be called with the results. The reason that all values ​​are the same is related to JavaScript call-by-sharing , that is, if you change the function parameter itself, then it won’t affect the element that was submitted to the parameter. If you change the internals of the parameter, it will be transferred to the element.

More details here .

+6
source

async.auto is a very useful and powerful feature provided by Async Lib.it has 3 fields 1-task 2- concurrency 3-callback

In Async.auto, each function depends on its parent function, except for the first function if any function receives any error at runtime. Then their child function, or say .. their function below will no longer be executed, the error will be caused by a callback, and the main callback will immediately return with an error

1- Task: - Object 2- concurrency: - an optional integer to determine the maximum number of tasks that can be performed in parallel. By default, as much as possible. 3- callback: - return a response

exapmle -

  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)) } }) } }); } 
0
source

Source: https://habr.com/ru/post/1210712/


All Articles