Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat(helpers): add promisify method
Browse files Browse the repository at this point in the history
promisify method converts callback style methods to promises
  • Loading branch information
thetutlage committed Jun 6, 2017
1 parent 3b813c0 commit f6f7fb2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"lint": "standard",
"pretest": "npm run lint",
"posttest": "npm run coverage",
"test:local": "node ./node_modules/.bin/japa",
"test:local": "node --harmony-async-await ./node_modules/.bin/japa",
"test": "./node_modules/.bin/nyc npm run test:local",
"test:win": "node ./node_modules/japa-cli/index.js",
"test:win": "node --harmony-async-await ./node_modules/japa-cli/index.js",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"keywords": [
Expand Down
15 changes: 15 additions & 0 deletions src/Helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

const path = require('path')
const pify = require('pify')

/**
* The Adonis framework is the core module containing all the required
Expand Down Expand Up @@ -166,6 +167,20 @@ class Helpers {
tmpPath (toFile = '') {
return path.join(this._appRoot, '/tmp', toFile)
}

/**
* Promisify callback style functions
*
* @method promisify
*
* @param {Function} fn
* @param {Object} options
*
* @return {Promise}
*/
promisify (fn, options) {
return pify(fn, options)
}
}

module.exports = Helpers
6 changes: 6 additions & 0 deletions test/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const test = require('japa')
const path = require('path')
const fs = require('fs')

const Helpers = require('../src/Helpers')

Expand Down Expand Up @@ -74,4 +75,9 @@ test.group('Helpers', (group) => {
test('return path to a file inside tmp dir', (assert) => {
assert.equal(this.helpers.tmpPath('logs.txt'), path.join(__dirname, './tmp/logs.txt'))
})

test('promisify callback style functions', async (assert) => {
const readFile = this.helpers.promisify(fs.readFile)
await readFile(path.join(__dirname, '../package.json'))
})
})

0 comments on commit f6f7fb2

Please sign in to comment.