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

Is there any way to launch async operation in node js upon exit?

Is there any way to launch async operation in node js upon exit?

Problem

Node js module during the Operation requests some resources on remote service, that better be released when it exits. We know that there is very nice:

process.on('exit', function() {
    // ...
});

But then it is said that it won't wait for any async operations to complete. So the question is if there's any workaround (there should be some, since it's quite widespread usage case)? Maybe one could start separate process or something?..

Problem courtesy of: jayarjo

Solution

Only workaround I've seen is adding a wait loop and not finishing/returning from the .on('exit', function until a property has been updated globally. Totally a bodge-job design-wise, very bad practice, but I've seen it work for short calls (I think there is some timeout but I never bothered to look into the details).

Solution courtesy of: Bob Davies

Discussion

View additional discussion.



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

Share the post

Is there any way to launch async operation in node js upon exit?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×