question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parallel: How to pass parameter into task function?

See original GitHub issue

I 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:closed
  • Created 10 years ago
  • Comments:8

github_iconTop GitHub Comments

21reactions
jlfabercommented, Feb 21, 2014

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

2reactions
LiamGoodacrecommented, Nov 29, 2014

For those interested, the following are roughly equivalent:

f(a, b, c, d)

async.apply(f, a, b)(c, d)

var part = async.apply(f, a, b)
part(c, d)

f.bind(null, a, b)(c, d)

var part = f.bind(null, a, b)
part(c, d)

f.apply(null, [a, b, c, d])

f.call(null, a, b, c, d)

(I expanded the multiple calls of async.apply and f.bind just in case it wasn’t obvious.)

You can find more information about the specifics of these here:

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found