cb2promise
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -0,1 +1,16 @@ | ||
<a name="1.1.0"></a> | ||
# 1.1.0 (2017-04-28) | ||
* Add benchmark ([3a2e79c](https://github.com/kikobeats/cb2promise/commit/3a2e79c)) | ||
* Add legacy tests ([33e3c6d](https://github.com/kikobeats/cb2promise/commit/33e3c6d)) | ||
* Add missing dependency ([e3adc9c](https://github.com/kikobeats/cb2promise/commit/e3adc9c)) | ||
* Add optimization test ([316397f](https://github.com/kikobeats/cb2promise/commit/316397f)) | ||
* Avoid lint optimization test ([f778e53](https://github.com/kikobeats/cb2promise/commit/f778e53)) | ||
* Better implementation ([27353bf](https://github.com/kikobeats/cb2promise/commit/27353bf)) | ||
* Fix JSON ([6fc6f5a](https://github.com/kikobeats/cb2promise/commit/6fc6f5a)) | ||
* Update docs ([5bb107f](https://github.com/kikobeats/cb2promise/commit/5bb107f)) | ||
* Update travis builds ([645272a](https://github.com/kikobeats/cb2promise/commit/645272a)) | ||
<a name="1.0.7"></a> | ||
@@ -2,0 +17,0 @@ ## 1.0.7 (2017-04-28) |
38
index.js
'use strict' | ||
const mimicFn = require('mimic-fn') | ||
const sliced = require('sliced') | ||
function cb2promise () { | ||
let args = sliced(arguments) | ||
const fn = args.shift() | ||
let resolve | ||
let reject | ||
function cb2promise (fn) { | ||
const len = arguments.length | ||
const args = sliced(arguments, 1, len) | ||
function callbackHandle () { | ||
let err | ||
args = sliced(arguments) | ||
err = args.shift() | ||
return !err ? resolve.apply(null, args) : reject(err) | ||
}; | ||
const promise = new Promise(function (resolve, reject) { | ||
args[len - 1] = createCallback(resolve, reject) | ||
fn.apply(null, args) | ||
}) | ||
function promiseFactory (resolvePromise, rejectPromise) { | ||
resolve = resolvePromise | ||
reject = rejectPromise | ||
return fn.apply(null, args) | ||
mimicFn(promise, fn) | ||
return promise | ||
} | ||
function createCallback (resolve, reject) { | ||
return function (err) { | ||
if (err) return reject(err) | ||
const args = sliced(arguments, 1, arguments.length) | ||
return resolve.apply(null, args.length === 1 ? args : [args]) | ||
} | ||
} | ||
args.push(callbackHandle) | ||
return new Promise(promiseFactory) | ||
}; | ||
module.exports = cb2promise |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/Kikobeats/cb2promise", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"main": "index.js", | ||
@@ -29,9 +29,16 @@ "author": { | ||
"dependencies": { | ||
"mimic-fn": "~1.1.0", | ||
"sliced": "~1.0.1" | ||
}, | ||
"devDependencies": { | ||
"coffee-script": "latest", | ||
"bluebird": "latest", | ||
"coveralls": "latest", | ||
"es6-promisify": "latest", | ||
"mocha": "latest", | ||
"nanobench": "latest", | ||
"nyc": "latest", | ||
"pify": "latest", | ||
"should": "latest", | ||
"standard": "latest" | ||
"standard": "latest", | ||
"standard-markdown": "latest" | ||
}, | ||
@@ -42,9 +49,18 @@ "engines": { | ||
"files": [ | ||
"dist", | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "sh test/test.sh" | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls", | ||
"lint": "standard-markdown && standard index.js benchmark.js test", | ||
"optimization-test": "node --trace_opt --trace_deopt --allow-natives-syntax optimization-test.js", | ||
"pretest": "npm run lint", | ||
"test": "nyc mocha && npm run optimization-test" | ||
}, | ||
"license": "MIT" | ||
"license": "MIT", | ||
"standard": { | ||
"globals": [ | ||
"describe", | ||
"it" | ||
] | ||
} | ||
} |
@@ -9,4 +9,6 @@ # cb2promise | ||
> Converts whatever standard NodeJS callback function into ES6 standard promise. | ||
> It converts from standard NodeJS callback into a ES2015 Promise. | ||
I use this library across my project for make easy provide API's that works with callback and promise style. | ||
## Install | ||
@@ -18,16 +20,40 @@ | ||
## Bencharmk | ||
```bash | ||
$ node benchmark.js | ||
# cb2promise | ||
# 1,2,3,4,5,6,7,8,8,9,10 | ||
ok ~2.65 ms (0 s + 2649092 ns) | ||
# pify | ||
# 1 | ||
ok ~912 μs (0 s + 911730 ns) | ||
# es6-promisify | ||
# 1 | ||
ok ~967 μs (0 s + 966663 ns) | ||
# bluebird | ||
# 1 | ||
ok ~3.41 ms (0 s + 3412077 ns) | ||
all benchmarks completed | ||
ok ~7.94 ms (0 s + 7939562 ns) | ||
``` | ||
## Usage | ||
```js | ||
var cb2promise = require('cb2promise'); | ||
const cb2promise = require('cb2promise') | ||
var sampleFunction = function(done) { | ||
return done(null, 'hello world'); | ||
}; | ||
const callbackFn = function (message, done) { | ||
return done(null, message) | ||
} | ||
var promise = cb2promise(sampleFunction) | ||
const promise = cb2promise(callbackFn, 'hello world') | ||
promise().then(console.log); | ||
promise().then(console.log) | ||
// => hello world | ||
``` | ||
@@ -34,0 +60,0 @@ |
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
7670
62
2
10
21
+ Addedmimic-fn@~1.1.0
+ Addedmimic-fn@1.1.0(transitive)