Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from venables/revert-util-promisify
Revert `util.promisify` usage to continue supporting Node 6
- Loading branch information
Showing
6 changed files
with
1,215 additions
and
1,160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ node_js: | |
- "12" | ||
- "10" | ||
- "8" | ||
- "6" | ||
after_success: yarn run coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Takes an express-style middleware and returns a promise-based version (returning a Promise, | ||
* rather than calling the final argument as a callback) suitable for use with koa. | ||
* | ||
* @param {Function} middleware - The middleware to promisify | ||
* @returns {Function} - The middleware function updated to return a Promise, not call a callback | ||
*/ | ||
function koaHelmetPromisify (middleware) { | ||
return function (req, res) { | ||
return new Promise(function (resolve, reject) { | ||
middleware(req, res, function (err) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
return resolve(); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
module.exports = koaHelmetPromisify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const promisify = require('../lib/promisify'); | ||
const test = require('ava'); | ||
|
||
function passingMiddleware (req, res, next) { | ||
return next(); | ||
} | ||
|
||
function failingMiddleware (req, res, next) { | ||
return next(new Error('Expected Failure')); | ||
} | ||
|
||
test('returns a promisified version of the middleware which resolves the Promise on success', t => { | ||
let middleware = promisify(passingMiddleware); | ||
|
||
return middleware().then( | ||
() => { | ||
t.pass(); | ||
}, | ||
err => { | ||
t.fail(err); | ||
} | ||
); | ||
}); | ||
|
||
test('returns a promisified version of the middleware which rejects the Promise on failure', t => { | ||
let middleware = promisify(failingMiddleware); | ||
|
||
return middleware().then( | ||
() => { | ||
t.fail('Unexpected Success!'); | ||
}, | ||
() => { | ||
t.pass(); | ||
} | ||
); | ||
}); |
Oops, something went wrong.