Skip to content

Commit

Permalink
update to use native promises
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Dec 13, 2016
1 parent 04bd7a8 commit 4a5b994
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 87 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@ jspm_packages
*.orig
.idea
.DS_Store

# babel output
index.js
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# promisify-call

Promisify a function call so users can call a function with a callback or get a promise.
* **2.0** works only with native `Promise` so requires Node >= 4.0.
* **1.0** uses [Bluebird](www.bluebirdjs.com) and should work with older Node.js

## Installation

Expand Down
22 changes: 11 additions & 11 deletions promisify.js → index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Promise from 'bluebird';
const wc = require('with-callback')

/**
* Promisifies the call to <code>fn</code> if appropriate given the arguments.
Expand All @@ -10,19 +10,19 @@ import Promise from 'bluebird';
* @param {arguments} args Arguments
* @return {undefined|*|Promise} Promise if promisified
*/
function promisifyCall(ctx, fn, ...args) {
const lastIndex = args.length - 1;
const lastArg = args && args.length > 0 ? args[lastIndex] : null;
const cb = typeof lastArg === 'function' ? lastArg : null;
function promisifyCall (ctx, fn, ...args) {
const lastIndex = args.length - 1
const lastArg = args && args.length > 0 ? args[lastIndex] : null
const cb = typeof lastArg === 'function' ? lastArg : null

if (cb) {
return fn.apply(ctx, args);
return fn.apply(ctx, args)
}

return Promise.fromCallback(callback => {
args.push(callback);
fn.apply(ctx, args);
});
return wc(callback => {
args.push(callback)
fn.apply(ctx, args)
})
}

module.exports = promisifyCall;
module.exports = promisifyCall
50 changes: 13 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"name": "promisify-call",
"version": "1.0.0",
"version": "2.0.0",
"description": "Promisify a function call so users can call a function with a callback or get a promise",
"main": "index.js",
"scripts": {
"test": "npm run dist && ava -v",
"dist": "node ./node_modules/babel-cli/bin/babel.js promisify.js --out-file index.js",
"docs": "jsdoc2md \"promisify.js\" --heading-depth 3 --template readme.hbs > README.md",
"prepublish": "npm run dist"
"test": "ava -v",
"docs": "jsdoc2md \"index.js\" --heading-depth 3 --template readme.hbs > README.md"
},
"repository": {
"type": "git",
Expand All @@ -27,13 +25,16 @@
},
"homepage": "https://github.com/bojand/promisify-call#readme",
"dependencies": {
"bluebird": "^3.4.1"
"with-callback": "^1.0.2"
},
"devDependencies": {
"ava": "^0.16.0",
"babel-cli": "^6.11.4",
"babel-preset-es2015": "^6.13.2",
"xo": "^0.16.0"
"ava": "^0.17.0",
"babel-eslint": "^7.1.1",
"jsdoc-to-markdown": "^2.0.1",
"standard": "^8.5.0"
},
"engines": {
"node": ">=4"
},
"ava": {
"files": [
Expand All @@ -43,32 +44,7 @@
"directories": {
"test": "test"
},
"xo": {
"envs": [
"node",
"es6",
"mocha"
],
"esnext": true,
"space": true,
"rules": {
"babel/arrow-parens": 0,
"babel/object-curly-spacing": 0,
"babel/object-shorthand": 1,
"brace-style": [
0,
"1tbs",
{
"allowSingleLine": false
}
],
"no-else-return": 1,
"no-unused-vars": 1,
"no-prototype-builtins": 0,
"import/namespace": 0
},
"ignores": [
"test/**"
]
"standard": {
"parser": "babel-eslint"
}
}
2 changes: 2 additions & 0 deletions readme.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# promisify-call

Promisify a function call so users can call a function with a callback or get a promise.
* **2.0** works only with native `Promise` so requires Node >= 4.0.
* **1.0** uses [Bluebird](www.bluebirdjs.com) and should work with older Node.js

## Installation

Expand Down
66 changes: 33 additions & 33 deletions test/promisify.test.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import test from 'ava';
import test from 'ava'

import promisifyCall from '../';
const promisifyCall = require('../')

function testFn(param, fn) {
function testFn (param, fn) {
setTimeout(() => {
if (param.toLowerCase() === 'error') {
return fn(new Error(param));
return fn(new Error(param))
}
return fn(null, param.toUpperCase());
}, 50);
return fn(null, param.toUpperCase())
}, 50)
}

function uppercase(param, fn) {
return promisifyCall(this, testFn, ...arguments);
function uppercase (param, fn) {
return promisifyCall(this, testFn, ...arguments)
}

test.cb('should properly return success value - callback', t => {
uppercase('foo', (err, res) => {
t.ifError(err);
t.is(res, 'FOO');
t.end();
});
});
t.ifError(err)
t.is(res, 'FOO')
t.end()
})
})

test.cb('should properly return error value - callback', t => {
uppercase('error', (err, res) => {
t.truthy(err);
t.falsy(res);
t.end();
});
});
t.truthy(err)
t.falsy(res)
t.end()
})
})

test.cb('should properly return success value - promised using then()', t => {
uppercase('foo').then(res => {
t.is(res, 'FOO');
t.end();
});
});
t.is(res, 'FOO')
t.end()
})
})

test.cb('should properly return error value - promised using then()', t => {
uppercase('error').then(res => {
t.falsy(res);
t.falsy(res)
}).catch(err => {
t.truthy(err);
t.end();
});
});
t.truthy(err)
t.end()
})
})

test('should properly return success value - promised', async t => {
t.plan(1);
const res = await uppercase('foo');
t.is(res, 'FOO');
});
t.plan(1)
const res = await uppercase('foo')
t.is(res, 'FOO')
})

test('should properly return error value - promised', async t => {
t.throws(uppercase('error'));
});
t.throws(uppercase('error'))
})

0 comments on commit 4a5b994

Please sign in to comment.