I’ve been struggling for a couple of weeks trying to find the best pattern to implement this, but didn’t end up with a proper solution.
I have a back-end written in Node.js, which is required to perform a sort of initialization to gather some data from external APIs, before it can work properly.
The calls to the external APIs must be performed sequentially, as the data result of one call is required by the next one.
Furthermore I have a couple of these sort of “calls pipeline” which can run independently, on separate “threads”.
The last complication is that I need all these calls pipelines to be finished before I can consider completed the initialization phase and I finally can start with the regular work for the back-end (i.e. exposing some APIs).
I have investigated several ways.
For sure, using synchronous calls is the first thing that comes to mind, but we all know it is strongly discouraged.
Also using async functions with await doesn’t seem to work to me.
Actually the .then() chain already implements a sort of pipeline, but still it’s not clear how to “wait until all the pipelines are finished”.
I tried with chain of .then()
+ Promise.allSettled()
, but it didn’t work as expected (maybe the array of Promise you pass to this method must include all the Promises, even those you create inside each then()….).
I guess there should be some standard pattern / best practise to implement this, as to me it doesn’t seem to be so an uncommon scenario.
Any hint highly appreciated.
I’m using axios
as library for the HTTP calls (GET and POST).
–Thomas