Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Can I check whether an asynchronous function is running?

Can I check whether an asynchronous function is running?

Problem

I'm building a shell script that calls various functions. When all functions are done I'd like to close the database connection and exit.

If I could somehow get an event every time an asynchronous job starts and ends I could handle it, but is that possible?

Problem courtesy of: webjay

Solution

I am confused by your question, You could use the async module in this way:

var async = require('async');
var exec = require('child_process').exec;

async.parallel( [
    //Spawn slow proccesses
    function(cb){ exec('cat *.js bad_file | wc -l', cb ) }, 
    function(cb){ exec('cat *.js bad_file2 | wc -l', cb ) },
    function(cb){ exec('cat *.js bad_file3 | wc -l', cb ) }
], 
function( err ){
    //If there is an error, the callback returns an error will be put in "err"
    if( err != null )
        console.log( err ); 

    //All done, quit!
    require("process").exit();
} );
Solution courtesy of: Nican

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Can I check whether an asynchronous function is running?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×