Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware should be able to exit the loop #4

Open
stephenmathieson opened this issue Jan 5, 2014 · 8 comments
Open

Middleware should be able to exit the loop #4

stephenmathieson opened this issue Jan 5, 2014 · 8 comments

Comments

@stephenmathieson
Copy link

What if we complete our task half-way though the stack?

@ianstormtaylor
Copy link
Contributor

hmm whats the use case? im not sure that fits nicely in the middleware abstraction, usually you want the full stack

@stephenmathieson
Copy link
Author

hmm.. good example would be express:

var express = require('express');

var app = express();

app.use(function (req, res, next) {
  // logic here
  req.foo = {};
  next();
});

app.use(function (req, res) {
  return res.json(req.foo);
});

app.use(function (req, res, next) {
  throw new Error('shouldn\'t get here');
});

app.listen(3006);

@ianstormtaylor
Copy link
Contributor

i assume that would be handled by just not calling next? i think this would actually be a reason not to include the magicness of the sync arity functions. but i could be convinced to still add it

@stephenmathieson
Copy link
Author

i agree the arity stuff does introduce a whole bunch of magic, but i think most people are used to it by now. it's already being used to check if a middleware is an error handler, so not much would have to change.

yeah, i think just not calling next would be the easiest way to handle this.

@calvinfo
Copy link
Contributor

calvinfo commented Jan 7, 2014

yeah, agreed -- just not calling next seems like a good way to go to me.

@blakeembrey
Copy link
Contributor

I know this is super old now, but the ability to be able to call done is incredibly useful but likely outside the scope of the module. In personal projects of mine, I've utilised a middleware-esque stack (in reverse since that was simpler) in a similar fashion on the UI. Everything runs through middleware until a member can handle it and call done. A couple of examples:

  • Ajax request chain - the base xhr is registered with the ability for other middleware to modify the request before it gets there (add a proxy, alter data, etc.)
  • UIs - hitting a keyboard button will send the key combination through middleware and when multiple menus/modals/etc. are open it'll close the last registered instead of them all (each registered callback unregisters itself)
  • Persistence - you can register both pass through persistence (a cache) and/or override it completely by registering before the default persistence method

Express and friends don't need this sort of thing because the response is technically the done callback. There is no need for registering a callback after this occurs but it is possible to do using res.on('end'). All of these require a callback at the end to know when the stack is done running, which isn't possible when you omit next. Is this a pattern somewhere else already?

@matthewmueller
Copy link
Contributor

Though not ideal, you could call fn(new Error('done')) to jump down to the bottom.

@blakeembrey
Copy link
Contributor

FWIW, since the synchronous support update, it's actually difficult to now stop the flow anywhere along the chain making it unusable for middleware flows in something like express.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants