Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "proxmis", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "A generator for creating promises that can be used as node.js callbacks.", | ||
@@ -5,0 +5,0 @@ "main": "proxmis.js", |
var Promise = require('es6-promise').Promise; | ||
var proxmis = function (callback) { | ||
var proxmis = function (options) { | ||
var defer, callable; | ||
var callback; | ||
if (typeof options === 'function') { | ||
callback = options; | ||
} else { | ||
options = options || {}; | ||
callback = options.callback; | ||
} | ||
@@ -11,9 +17,15 @@ defer = new Promise(function (resolve, reject) { | ||
if (callback) { | ||
callback(err, data); | ||
callback.apply(this, arguments); | ||
} | ||
if (err) { | ||
reject(err); | ||
if (options.noError) { | ||
resolve(err); | ||
} else if (options.allArgs) { | ||
resolve(Array.prototype.slice.call(arguments)); | ||
} else { | ||
resolve(data); | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
} | ||
@@ -31,7 +43,11 @@ }; | ||
callable.catch = function () { | ||
return defer.catch.apply(defer, arguments); | ||
}; | ||
return callable; | ||
}; | ||
proxmis.wrap = function (actionable) { | ||
var proxy = proxmis(); | ||
proxmis.wrap = function (actionable, options) { | ||
var proxy = proxmis(options); | ||
actionable(proxy); | ||
@@ -41,2 +57,2 @@ return proxy; | ||
module.exports = proxmis; | ||
module.exports = proxmis; |
@@ -32,2 +32,28 @@ Proxmis.js | ||
The first argument on `proxmis()` can optionally be another callback to be invoked before the promise resolves, or an object containing extra options for how the promise will be resolved. | ||
```js | ||
function fileStatWithCallbackOrPromise(cb) { | ||
var prox = proxmis(cb); | ||
fs.stat('/var', prox); | ||
return prox; | ||
} | ||
``` | ||
```js | ||
var prox = proxmis({noError: true}); | ||
prox('first argument resolves', 'all other arguments ignored'); | ||
prox.then(function (result) { | ||
// result = 'first argument resolves' | ||
}); | ||
``` | ||
```js | ||
var prox = proxmis({allArgs: true}); | ||
prox('each argument resolves', 'together as', 'an array'); | ||
prox.then(function (result) { | ||
// result = ['each argument resolves', 'together as', 'an array'] | ||
}); | ||
``` | ||
Proxmis also provides a routine for wrapping a traditional call in a closure, directly returning a promise. | ||
@@ -43,2 +69,2 @@ | ||
Proxmis uses the [ES6 Promise Polyfill](https://github.com/jakearchibald/es6-promise) to generate the promise it returns. | ||
Proxmis uses the [ES6 Promise Polyfill](https://github.com/jakearchibald/es6-promise) to generate the promise it returns. |
63
test.js
@@ -174,3 +174,66 @@ | ||
exports['Promise Rejects into .catch()'] = function (test) { | ||
test.expect(1); | ||
var prox = proxmis(); | ||
prox.catch(function (error) { | ||
test.equal(error, 'Failure'); | ||
test.done(); | ||
}); | ||
prox('Failure', 10); | ||
}; | ||
exports['Promise Resolves from an errorless callback'] = function (test) { | ||
test.expect(1); | ||
var prox = proxmis({noError: true}); | ||
prox.then( | ||
function (result) { | ||
test.equal(result, 20); | ||
test.done(); | ||
}, | ||
function (error) { | ||
test.ok(false, 'Promise Rejected'); | ||
test.done(); | ||
} | ||
); | ||
prox(20, 10); | ||
}; | ||
exports['Promise Resolves with all arguments'] = function (test) { | ||
test.expect(1); | ||
var prox = proxmis({allArgs: true}); | ||
prox.then( | ||
function (result) { | ||
test.deepEqual(result, [20, 10]); | ||
test.done(); | ||
}, | ||
function (error) { | ||
test.ok(false, 'Promise Rejected'); | ||
test.done(); | ||
} | ||
); | ||
prox(20, 10); | ||
}; | ||
exports['Wrapper supports options'] = function (test) { | ||
test.expect(2); | ||
proxmis.wrap(function (callback) { | ||
test.ok('Invoked'); | ||
callback(10); | ||
}, {noError:true}).then( | ||
function (result) { | ||
test.equal(result, 10); | ||
test.done(); | ||
}, | ||
function (error) { | ||
test.ok(false, 'Promise Rejected'); | ||
test.done(); | ||
} | ||
); | ||
}; |
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
8478
245
69