I am building a Node app and a problem I face is that somewhere when my server I am running a promise chain, and it breaks due to some parameter being invalid via Mongo or some goddamn reason, the promise chain breaks midway, remaining database calls don't execute, Making it a complete shit storm to come out of.
Solution for this could be various, 1. Better testing, 2. Better design of backend,
but I am asking, is there a way to manage state in NodeJS such that if in my series of database calls/promise calls, it fails midway I clean up everything and go back to a clean state. It always stays in one of the determined state and never move away of it.
Has anybody attempted to bring this kind of robustness in their node code?
Well that's the brute force solution!!! Still it does the job. Thanks!! Will try and implement this. I was thinking more like a state based solution. Like your program will have states, based on certain conditions and when those are fulfilled it enters a participating state and does the required action, in case it fails anywhere. All actions done up and until that point are reverted and system restored to previous condition, waiting for next input to come.
Lars
German developer, who likes to play with everything that comes in his way
You could catch the failing promise.
Something like this. Not tested.
start().then(result => something()) .then(result => thisFails()) // this catch is called .catch(error => cleanup()) // after the catch the chain goes on .then(result => success()) // this exception is ignored .catch(error => cleanup()) // also called .then(result => final()) ...not very elegant but it could work.