Can you execute async function while process is exiting?

If not, is there any way around to do this? What I want to do is connect to external server when my app is closing, but connecting is asynchronous so I was wondering if it’s actually possible. ASynchronous functions don’t stop code execution so I think it’s not possible, eh?

There are in fact many ways to do this! Could we see some code? (especially the exiting areas)

1 Like

function wait(condition) {
if(condition() == false) {
setTimeout(wait, 1e2, condition);
function connect(callback) {
let websocket = new WebSocket(“wss://”);
wait(function() {
if(websocket.readyState > 0) {
if(websocket.readyState == 1) {
websocket.send(new Uint8Array([0]));
return true;
return false;
for(let i of [“exit”, “uncaughtException”, “SIGINT”]) {
process.on(i, function() {
connect(function() {
setTimeout(process.exit, 1e3)

that’s what I was trying to work with, but after I’m setting up the timeout in wait() function, the process exits without a word.
oh yeah also, if want to know, for the WebSocket function I’m using library called “ws”.
Did not use

websocket.on(“open”, function() {});

because I was aware of connecting probably not being able to stop process from exiting.
Isn’t ability to allow async functions on process exit a bit sketchy? Doesn’t this mean process can run infinitely long?

My question is why are you connecting right before disconnecting? Once your process exits the connections will disconnect shortly after

1 Like

I want to connect to a server and send whatever information so it knows I’m going down. I don’t want to keep the connection alive. All I want is to stop process from exiting before I do connect and send anything.

So if I am correct, you want the websocket to connect to all the servers, send a packet and after that’s done, exit the process?

1 Like

Is there a recursion problem in the code … the handler for process.exit() is calling process.exit().

For the code to exit itself, there’s no need to invoke an exit handler, it can have an exit function that runs process.exit().

For SIGINT, if its being sent by the project container shutting down, there is a small grace period before the shutdown is forced. Its unlikely the process will have time to connect to an external process.
Also the console.log likely won’t show, as the console would also have received an SIGINT.

Yes. In fact I want to connect to only one server.
If it’s any trouble for you to either understand my code or fix it in a reasonable way, I’m opened to new code ideas. It’s no problem for me, so if you have any problem, consider just giving me a fresh example.

Wait. I have just realised it actually works. It does timeout as many functions as it needs to, until the result of websocket.readyState is in fact greater than 0, and it manages to connect and send stuff. Just that it doesn’t work when you do process.exit() yourself, it only works when the project refreshes itself after some code has been changed.
I’m so sorry for the trouble lmao. Wouldn’t think there is much difference between these both.

1 Like

It is calling process.exit() upon websocket “open” event. Not before. I checked the other server it was supposed to connect to, but no packet was registered. It closes right when the timeout is set (though recently I’ve noticed its doing the timeout a couple of times which is a fairly good sign, I don’t know why it didn’t work before, probably because I emulated process.exit myself, and this time it was the project that was restarting (because of change in code). It said “connecting”, but did not say “connected”).