-1

I found it frustrating to do function ***(req, res){ ... } every time when working with promises. So I had an idea of making a top, reference objects instead, Req and Res

let Req;
let Res; 

// i can also do "const { username } = Req.body" to make "username" avaliable to all functions

function promise() {  return Promise.resolve(); }
function promise2() {  Res.send('test');  }

module.exports = (req, res, next) =>  { 
Req = req
Res = res
return promise().then(() => promise2()); 
}

What are your thoughts on this, pros and cons? If this is anti-patern let me know and suggest a preferred way.

Thanks

Antartica
  • 125
  • 9
  • You do not want to do this. You're making a highly scoped resource hoisted to the global scope which will frequently change. – Sterling Archer Sep 13 '16 at 20:44
  • @SterlingArcher please clarify "which will frequently change" – Antartica Sep 13 '16 at 20:53
  • Global variables are a very very bad idea. – Bergi Sep 13 '16 at 20:54
  • 2
    Think of it like this, somebody else uses that export after you. Suddenly, your `Req` and `Res` are connected to the other person's session. That is what I mean. – Sterling Archer Sep 13 '16 at 20:54
  • I have no idea what `req` and `res` have to do with promises? – Bergi Sep 13 '16 at 20:54
  • @SterlingArcher @Bergi ... so you suggest either passing `(req, res)` to functions or wrapping `module.exports` around functions? Which is more preferred? – Antartica Sep 13 '16 at 20:59
  • @Atartica You only need to pass the relevant parts of `req` to the functions, and write to `res` in the last callback in the chain. – Bergi Sep 13 '16 at 21:02
  • @Bergi yeah, but the thing is in some cases I want to have shared data such as `const { username } = req.body`, so in this case the best way is to wrap functions inside `module.exports` to make `req, res` available to all functions? – Antartica Sep 13 '16 at 21:06
  • @Atartica Not sure what you mean by "shared data", if you want to pass something down the chain just resolve your promise with it. No, making `res` and `req` globally available to all functions is a bad idea. – Bergi Sep 13 '16 at 21:11
  • @Bergi by shared I meant having an object that can be referenced in all functions. Without having to do `const { username } = req.body`in every function. Sometimes more that one function relies on objects... Ei: `module.exports = (req, res, next) => { const { username } = req.body; // post data "bodyParser" function fun(req, res) { console.log(username); } function fun2(req, res) { console.log(username); } }` – Antartica Sep 13 '16 at 21:23
  • @Atartica That will work only for local functions that are part of the exported function. For functions that will be exported and invoked separately themselves it cannot work. But really, if you use promises you should drop at least `res` and `next` from all the parameter lists. – Bergi Sep 13 '16 at 21:39
  • Pretty much the same answer as this one: [Access current req object everywhere in Node.js Express](http://stackoverflow.com/questions/39339640/access-current-req-object-everywhere-in-node-js-express/39383074#39383074). Unless you plan on NEVER having more than one user use your server, using shared variables for request-specific data is a horrible idea. – jfriend00 Sep 13 '16 at 21:42
  • @jfriend00 thanks, you said `There are some circumstances where you may be able to use a closure to "capture" the desired req object and then use it in inner functions without passing it to those inner functions`. Is `closure`, slower than passing `(req, res)` to functions? Since functions outside the `closure` would be cached – Antartica Sep 13 '16 at 22:39
  • 1
    Pick the programming structure that works best for you and worry about micro-optimizing performance a long time in the future only if profiling shows you actually have a meaningful performance issue in this code. Don't avoid good programming techniques because you think something else "might" be a smidge faster. There is no generic answer about performance of a closure - it depends entirely upon the specific situation of the code and exact code would need to be tested to know. FYI, a good interpreter can cache some inner functions too. – jfriend00 Sep 13 '16 at 22:42
  • Thanks @jfriend00 for directions :) – Antartica Sep 13 '16 at 22:47

1 Answers1

1

Instead of having every promise to resolve with req and res (would need to be in a single object, because you can only resolve one thing).

You can use use this pattern.

function promise1(req, res) {
  req.whatever = '';           // will be available in future promises
  return Promise.resolve(foo); // you don't need to resolve req and res 
}

function promise2(req, res) {
  return function (foo) {
    // whatever promise1 resolved to, will be here :)
    res.send('test');
  }
}

module.exports = (req, res, next) =>  {
  return promise1(req, res)
    .then(promise2(req, res));
}

In this case you are not hoisting your Req and Res variables in the global scope of your module (you really don't need it to be global in this case)

juanmaia
  • 56
  • 3