1

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?..

jayarjo
  • 16,124
  • 24
  • 94
  • 138

2 Answers2

1

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).

Bob Davies
  • 2,229
  • 1
  • 19
  • 28
1

I think you could/should do clean-up before on('exit') by listening for ctrl-c signal like in this post.

Community
  • 1
  • 1
Alfred
  • 60,935
  • 33
  • 147
  • 186
  • the `SIGINT` event is raised on `ctrl+c`, placing an async operation and invoking the `exit` event within the async's`.then` would probably work, but what about crashes of the system? it won't raise the `SIGINT` event but the `exit` and the async operation won't be invoked, any ideas? – Kesem David Nov 10 '16 at 13:34