I have an express server which needs to fetch some data from multiple external sources for each request. This logic is seperated into multiple routers (some are not managed by me).
These routers are completely independent, so there is no need for one to wait on the other.
As an example I have the following code:
const router1 = express.Router();
const router2 = express.Router();
const router3 = express.Router();
const finalRouter = express.Router();
router1.use((req, res, next) => setTimeout(next, 2000));
router2.use((req, res, next) => setTimeout(next, 2000));
router3.use((req, res, next) => setTimeout(next, 2000));
finalRouter.use((req, res, next) => console.log('All done!'));
When I would normally use all these routers in my application, it will execute sequentially and print All done!
in 6 seconds.
But to improve the speed of my page I want to execute them in parallel, so they are all finished in 2 seconds. How can I do this?