Parallel: How to pass parameter into task function?
See original GitHub issueI need to pass variable to parallel tasks. How do I do that? I’m trying to escape a callback hell and have them nicely organized in a file. How would I make this
async.parallel({
profile_data: function(callback) {
console.log('running getUserInfo');
api.get_user_info({
uToken: token
}, function(response) {
if (response['data']) {
callback(null, response['data']);
return;
}
callback('not profile', response);
});
},
notification_data: function(callback) {
console.log('running getUserNot');
api.get_user_notification_summary({
uToken: token
}, function(response) {
if (response['data']) {
callback(null, response['data']);
return;
}
callback('not notifications', response);
});
}
}, function(err, results) {
if (err !== null) {
console.log(err, results);
} else {
res.locals.session = results['profile_data'];
res.locals.session.notification = results['notification_data'];
}
next();
});
to this
var getProfileData = function(token, cb){
...
cb();
}
var getNotificationData = function(token, cb){
...
cb();
}
async.parallel({
profile_data: getProfileData,
notification_data: getNotificationData
}, function(err, result){
console.log(result['profile_data'], result['notification_data']);
});
I don’t know how to pass token into each task…
Issue Analytics
- State:
- Created 10 years ago
- Comments:8
Top Results From Across the Web
Task.Run with Parameter(s)? - Stack Overflow
There are only 2 things similiar: static Task Run(Action action) and static Task Run(Func<Task> function) but can't post parameter(s) with both.
Read more >Correct way to provide parameter to C# Task - Dot Net For All
In this article I will discuss the correct way to provide input parameter to the task and not to use the shared variable...
Read more >How to start a task that takes a parameter and returns a value?
I'm trying to use Task.Factory.StartNew to start a task that both takes a parameter and returns a value, and I can't seem to...
Read more >Use task mapping to map over a specific set of arguments
Mapping in Prefect is a great way for dynamic task creation and parallel execution. Sometimes you may want to pass extra arguments to...
Read more >Parallel - AWS Step Functions
Learn about the AWS Step Functions Parallel state. ... Pass a collection of key value pairs, where the values are static or selected...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
async.parallel({ profile_data: getProfileData.bind(null, token), notification_data: getNotificationData.bind(null, token) }, function (err, result) { console.log(result.profile_data, result.notification_data); });
For those interested, the following are roughly equivalent:
(I expanded the multiple calls of
async.apply
andf.bind
just in case it wasn’t obvious.)You can find more information about the specifics of these here: