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

How to write a node.js function that waits for an event to fire before 'returning'?

How to write a node.js function that waits for an event to fire before 'returning'?


I have a node application that is not a web application - it completes a series of asynchronous tasks before returning 1. Immediately before returning, the results of the program are printed to the console.

How do I make sure all the asynchronous work is completed before returning? I was able to achieve something similar to this in a web application by making sure all tasks we completed before calling res.end(), but I haven't any equivalent for a final 'event' to call before letting a script return.

See below for my (broken) Function currently, attempting to wait until callStack is empty. I just discovered that this is a kind of nonsensical approach because node waits for processHub to complete before entering any of the asynchronous functions called in processObjWithRef.

function processHub(hubFileContents){
    var callStack = [];
    var myNewObj = {};
    processObjWithRef(samplePayload, myNewObj, callStack);
        //do nothing
    return 1

Note: I have tried many times previously to achieve this kind of behavior with libraries like async (see my related question at How can I make this call to request in nodejs synchronous?) so please take the answer and comments there into account before suggesting any answers based on 'just use asynch'.

Problem courtesy of: Trindaz


The problem is with your design of the function. You want to return a synchronous result form a list of tasks that are executed asynchronously.

You should implement your function with an extra parameter that will be the callback where you would put the result (in this case, 1) for some consumer to do something with it.

Also you need to have a callback parameter in your inner function, otherwise you won't know when it ends. If this last thing is not possible, then you should do some kind of polling (using setInterval perhaps) to test when the callStack array is populated.

Remember, in Javascript you should never ever do a busy wait. That will lock your program entirely as it runs on a single process.

Solution courtesy of: Leonardo Garcia Crespo


View additional discussion.

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

Share the post

How to write a node.js function that waits for an event to fire before 'returning'?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription