I'm working on a Node.js command line program with many sub-commands that do various system tasks, and also does SQL queries using node-postgres.Every once in a while I run into various issues when the process is finishing, due to there still being some postgres queries that haven't finished running, or some other type of promise hasn't finished yet. The problem will generally either be:The process stalls at the end, even though there's nothing left to do... sometimes the stall is like 10 seconds or so, other times it's forever (until I hit ctrl-c)Or... I'm killing the postgres connection/client/pool or other async task before they get a chance to finish their workIs there some simple way to get an answer to: Are there any unresolved promises anywhere at in the current Node process? (without having access to all the promises themselves).Basically I want to do some tidying up things (such as closing database connections) at the end of the main script, but only after all promises that might be anywhere in the app have finished.Maybe this is more specifically a node-postgres question... I'm not sure, but I've seen it with other non-db stuff too. I have a feeling that I have a lot more to learn about the details behind how async/promises work, but I have trouble finding relevant info beyond the the basics.Right now I've been wondering if I need to keep some bigass array, or new Set() to keep track of them and remove them, with some setTimeout() loop to wait until it's empty before closing the connection etc. But that seems kinda like some crazy workaround based on me not understanding things.Any suggestions? Either specifically to the node-postgres / end of process thing, or even just tips on getting a better understand of how all this stuff works? I've read a million "intro to async/await/promises" guides, but maybe I need something more detailed that is specific to Node?
Submitted April 20, 2020 at 12:19PM by r0ck0
No comments:
Post a Comment