express-async-handler
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -1,1 +0,8 @@ | ||
declare module 'express-async-handler' | ||
import express = require('express'); | ||
declare function expressAsyncHandler(handler: express.RequestHandler): express.RequestHandler; | ||
declare namespace expressAsyncHandler { | ||
} | ||
export = expressAsyncHandler; |
const asyncUtil = fn => | ||
function asyncUtilWrap(req, res, next, ...args) { | ||
const fnReturn = fn(req, res, next, ...args) | ||
if (fnReturn instanceof Promise) { | ||
return fnReturn.catch(next) | ||
} else { | ||
return fnReturn | ||
} | ||
return Promise.resolve(fnReturn).catch(next) | ||
} | ||
module.exports = asyncUtil |
{ | ||
"name": "express-async-handler", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Express Error Handler for Async Functions", | ||
@@ -23,2 +23,5 @@ "main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@types/express": "*" | ||
}, | ||
"devDependencies": { | ||
@@ -25,0 +28,0 @@ "chai": "^4.1.2", |
25
test.js
@@ -75,2 +75,27 @@ const chai = require('chai') | ||
}) | ||
// NB, thenables are not guaranteed to have a `catch` method. | ||
it('should handle thenables', async () => { | ||
const error = Error('catch me!') | ||
// construct a minimalist thenable which we can fail at a specific time | ||
let thenable, triggerFailure | ||
const registeringThenable = new Promise(res => { | ||
thenable = { | ||
then: sinon.spy((success, fail) => { | ||
triggerFailure = fail | ||
res() | ||
}) | ||
} | ||
}) | ||
// test the actual library feature | ||
const next = sinon.spy() | ||
const catchingThenable = asyncUtil(_ => thenable)(null, null, next) | ||
await registeringThenable | ||
expect(thenable.then).to.have.been.called | ||
expect(next).not.to.have.been.called | ||
triggerFailure(error) | ||
await catchingThenable | ||
expect(next).to.have.been.calledWith(error) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12698
7
93
1
+ Added@types/express@*
+ Added@types/body-parser@1.19.5(transitive)
+ Added@types/connect@3.4.38(transitive)
+ Added@types/express@5.0.0(transitive)
+ Added@types/express-serve-static-core@5.0.1(transitive)
+ Added@types/http-errors@2.0.4(transitive)
+ Added@types/mime@1.3.5(transitive)
+ Added@types/node@22.9.1(transitive)
+ Added@types/qs@6.9.17(transitive)
+ Added@types/range-parser@1.2.7(transitive)
+ Added@types/send@0.17.4(transitive)
+ Added@types/serve-static@1.15.7(transitive)
+ Addedundici-types@6.19.8(transitive)