Skip to content

Commit 2226ece

Browse files
authored
feat: deprecate non-native Promises (#154)
Signed-off-by: Jon Koops <[email protected]>
1 parent 79beed1 commit 2226ece

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const { METHODS } = require('node:http')
1818
const parseUrl = require('parseurl')
1919
const Route = require('./lib/route')
2020
const debug = require('debug')('router')
21+
const deprecate = require('depd')('router')
2122

2223
/**
2324
* Module variables.
@@ -647,6 +648,10 @@ function processParams (params, layer, called, req, res, done) {
647648
try {
648649
const ret = fn(req, res, paramCallback, paramVal, key)
649650
if (isPromise(ret)) {
651+
if (!(ret instanceof Promise)) {
652+
deprecate('parameters that are Promise-like are deprecated, use a native Promise instead')
653+
}
654+
650655
ret.then(null, function (error) {
651656
paramCallback(error || new Error('Rejected promise'))
652657
})

lib/layer.js

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
const isPromise = require('is-promise')
1616
const pathRegexp = require('path-to-regexp')
1717
const debug = require('debug')('router:layer')
18+
const deprecate = require('depd')('router')
1819

1920
/**
2021
* Module variables.
@@ -116,6 +117,10 @@ Layer.prototype.handleError = function handleError (error, req, res, next) {
116117

117118
// wait for returned promise
118119
if (isPromise(ret)) {
120+
if (!(ret instanceof Promise)) {
121+
deprecate('handlers that are Promise-like are deprecated, use a native Promise instead')
122+
}
123+
119124
ret.then(null, function (error) {
120125
next(error || new Error('Rejected promise'))
121126
})
@@ -148,6 +153,10 @@ Layer.prototype.handleRequest = function handleRequest (req, res, next) {
148153

149154
// wait for returned promise
150155
if (isPromise(ret)) {
156+
if (!(ret instanceof Promise)) {
157+
deprecate('handlers that are Promise-like are deprecated, use a native Promise instead')
158+
}
159+
151160
ret.then(null, function (error) {
152161
next(error || new Error('Rejected promise'))
153162
})

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"repository": "pillarjs/router",
1111
"dependencies": {
1212
"debug": "^4.4.0",
13+
"depd": "^2.0.0",
1314
"is-promise": "^4.0.0",
1415
"parseurl": "^1.3.3",
1516
"path-to-regexp": "^8.0.0"

0 commit comments

Comments
 (0)