Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async.parallel for app.use with second parameter

I use express v4.17.1 and would like to make my app.use() middlewares to run in parallel.

So I have found some examples in the internet.

Example:

function runInParallel() {
  async.parallel([
    getUserProfile,
    getRecentActivity,
    getSubscriptions,
    getNotifications
  ], function(err, results) {
    //This callback runs when all the functions complete
  });
}

But what I have in my application is:

const app = express();
const APP_FOLDER = "bdt";

app.use(httpContext.middleware);
app.use(metricsMiddleware);
app.use(rtEndMiddleware);
app.use(trackingContextMiddleware);
app.use(healthRoutes());

app.use("/" + APP_FOLDER + "/api/products", productsRoutes);
app.use("/tcalc/" + APP_FOLDER + "/api/products", productsRoutes);

productRoutes is this:

const jsonParser = bodyParser.json({
  limit: "1mb",
});

const accessFilter = accessFilterMiddleware(Registry.list());
const localDevFilter = localDevFilterMiddleware(Registry.list());

const apiRoutes: Router = Router();


apiRoutes.get("/", listProducts);
apiRoutes.get("/healthz", cProductHealth);
apiRoutes.get("/:id", accessFilter, localDevFilter, fetchProductData);
apiRoutes.post(
  "/:id",
  accessFilter,
  localDevFilter,
  jsonParser,
  fetchProductData,
);
apiRoutes.get(
  "/:id/fields/:fieldId/options",
  accessFilter,
  localDevFilter,
  fetchProductOptions,
);
apiRoutes.post(
  "/:id/loadmoreoptions",
  accessFilter,
  localDevFilter,
  jsonParser,
  loadMoreOptions,
);
apiRoutes.post("/:id/ploy", accessFilter, jsonParser, fetchMultipleProductData);
apiRoutes.post(
  "/:id/gxx",
  accessFilter,
  localDevFilter,
  jsonParser,
  fetchGssData,
);
apiRoutes.get("/:id/healthz", collectProductHealth);

I think for the first ones it should be easy:

async.parallel([
  httpContext.middleware,
  metricsMiddleware,
  rtEndMiddleware,
  trackingContextMiddleware,
  healthRoutes()
], function(err, results) {
  //This callback runs when all the functions complete
});

But my question: How can I do it with a second parameter (productRoutes) in this case ?

app.use("/" + APP_FOLDER + "/api/products", productsRoutes);
app.use("/tcalc/" + APP_FOLDER + "/api/products", productsRoutes);
like image 502
Gutelaunetyp Avatar asked May 17 '26 19:05

Gutelaunetyp


1 Answers

You've been going down a rabbit hole and you should change your approach quite a bit, currently you're completely over complicating a simple problem and the solution you're looking for is not possible in javascript.

If you want to create routes in express then don't use app.use for everything, it should only be used for middleware or to register a router, on which you can define routes.

You should be using:

app.get('/', () => ...

To define your routes. Alternatively you can use a router for that as well by doing:

app.use(router)

...

router.get('/', () => ...

More than that if you want to define async or "parallel" routes in javascript then just define async callbacks like normal, remove most of the stuff you've done.

app.get('/', async () => ...

That is now a route that will execute asynchronously.

You should also be careful not to just mess around with express' middleware because you're going to mess up the existing middleware (like error routes).

What's more is that the library you're referring to is just a helper library with neat functionality, it won't fundamentally change how javascript works. When you call an async function it will be added to the event queue and still be executed synchronously one after the other in serial, true multi threading isn't possible except for service workers and browser API calls executing temporarily in a single thread of their own, before being added to the event queue anyways.

What you're looking for is just a simple: router.get('/', async () => ..., that's the best you can do and it will appear that all your routes are executing in parallel.

After you've declared multiple of those then you can invoke all of them with something like Promise.all. My best guess is that this is what something like parallel is doing as well.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!