Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is there any way to set timeout in parallel? #429

Closed
stoneshim opened this issue Dec 23, 2013 · 2 comments
Closed

is there any way to set timeout in parallel? #429

stoneshim opened this issue Dec 23, 2013 · 2 comments

Comments

@stoneshim
Copy link

I want to run multiple functions in parallel, in specified time.
If timeout is occurred, i want to get completed results at the moment. (except the results of uncompleted functions at the moment)
Is there any way to do that?

In my opinion, if i need partial results (completed results of functions before timeout is occurred) when timeout is occurred, setTimeout() must be in async function.

I need function like this...
async.parallelInTime(tasks, timeout, [callback]);

What do you think about this?

async.parallelInTime({ 
    one: function(callback){ 
        setTimeout(function(){ 
            console.log('1'); 
            callback(null, 1); 
        }, 400); 
    }, 
    two: function(callback){ 
        setTimeout(function(){ 
            console.log('2'); 
            callback(null, 2); 
        }, 200); 
    } 
}, 
300, // timeout in mille seconds.
function(err, results) { 
    if(err) { 
        console.log(err); // [Error: Timeout occurred.]
    } 
    console.log(results); // { two: 2 }
}); 
@caolan caolan closed this as completed Mar 28, 2014
@giorgosera
Copy link

Why is this closed? Is there a way to do this because I have a similar issue in my app.

I'd like to set a timeout to force the execution to halt. I'm making parallel requests in different webservers and if they do not reply back within a deadline I want to stop the execution and get the results up to now.

I sort of hacked this using a setTimeout in a dummy function in which I return an error to cause the execution to halt and get the results. It'll be much better if we could have a timeout parameter.

async.parallel({ 
    one: function(callback){
        //Call webserver1 and pass the response 
        callback(null, res1); 
    }, 
    two: function(callback){ 
        //Call webserver2 and pass the response 
        callback(null, res2); 
    },
    dummy: function(callback){
        //Intentianally return an error after a timeout to force it to stop
        setTimeout(function(){  
            callback(new Error('This halts the parallel execution'), null); 
        }, 200); 
   } 
}, 
function(err, results) { 
    console.log(results);
}); 

@caolan
Copy link
Owner

caolan commented Apr 10, 2014

It should be easy enough to come up with a timeout wrapper for any callback interface. I'd even be open to including that in async.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants