Skip to main content
Commonmark migration
Source Link

And never calling then() on this returned Promise. Is this pattern used because we aren't relying on any returned value from middleware functions?

 

Is it OK to just return Promises and never call then() on them to get their value, since there is no meaningful value returned from middleware in Express?

And never calling then() on this returned Promise. Is this pattern used because we aren't relying on any returned value from middleware functions?

 

Is it OK to just return Promises and never call then() on them to get their value, since there is no meaningful value returned from middleware in Express?

And never calling then() on this returned Promise. Is this pattern used because we aren't relying on any returned value from middleware functions?

Is it OK to just return Promises and never call then() on them to get their value, since there is no meaningful value returned from middleware in Express?

added 149 characters in body
Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k
const express = require('express'); app.get(somePath, someFunc); 
const express = require('./express-p.js'); app.getP(somePath, someFunc); 

Then, you can freely use theany of these methods and they automatically handle rejected promises returned from routes:

const express = require('express'); 
const express = require('./express-p.js'); 

Then, you can freely use the methods:

const express = require('express'); app.get(somePath, someFunc); 
const express = require('./express-p.js'); app.getP(somePath, someFunc); 

Then, you can freely use any of these methods and they automatically handle rejected promises returned from routes:

added 1959 characters in body
Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k

Here's a more advanced implementation. Put this in a file called express-p.js:

const express = require('express'); // promise-aware handler substitute function handleP(verb) { return function (...args) { function wrap(fn) { return async function(req, res, next) { // catch both synchronous exceptions and asynchronous rejections try { await fn(req, res, next); } catch(e) { next(e); } } } // reconstruct arguments with wrapped functions let newArgs = args.map(arg => { if (typeof arg === "function") { return wrap(arg); } else { return arg; } }); // register actual middleware with wrapped functions this[verb](...newArgs); } } // modify prototypes for app and router // to add useP, allP, getP, postP, optionsP, deleteP variants ["use", "all", "get", "post", "options", "delete"].forEach(verb => { let handler = handleP(verb); express.Router[verb + "P"] = handler; express.application[verb + "P"] = handler; }); module.exports = express; 

Then, in your project, instead of this:

const express = require('express'); 

use this:

const express = require('./express-p.js'); 

Then, you can freely use the methods:

 .useP() .allP() .getP() .postP() .deleteP() .optionsP() 

On either an app object you create or any router objects you create. This code modifies the prototypes so any app object or router objects you create after you load this module will automatically have all those promise-aware methods.


Here's a more advanced implementation. Put this in a file called express-p.js:

const express = require('express'); // promise-aware handler substitute function handleP(verb) { return function (...args) { function wrap(fn) { return async function(req, res, next) { // catch both synchronous exceptions and asynchronous rejections try { await fn(req, res, next); } catch(e) { next(e); } } } // reconstruct arguments with wrapped functions let newArgs = args.map(arg => { if (typeof arg === "function") { return wrap(arg); } else { return arg; } }); // register actual middleware with wrapped functions this[verb](...newArgs); } } // modify prototypes for app and router // to add useP, allP, getP, postP, optionsP, deleteP variants ["use", "all", "get", "post", "options", "delete"].forEach(verb => { let handler = handleP(verb); express.Router[verb + "P"] = handler; express.application[verb + "P"] = handler; }); module.exports = express; 

Then, in your project, instead of this:

const express = require('express'); 

use this:

const express = require('./express-p.js'); 

Then, you can freely use the methods:

 .useP() .allP() .getP() .postP() .deleteP() .optionsP() 

On either an app object you create or any router objects you create. This code modifies the prototypes so any app object or router objects you create after you load this module will automatically have all those promise-aware methods.

deleted 1 character in body
Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k
Loading
added 1075 characters in body
Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k
Loading
Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k
Loading