Comparing version 0.1.1 to 0.1.2
11
kew.js
@@ -15,4 +15,10 @@ /** | ||
this._failFn = onFail | ||
this._context = undefined | ||
} | ||
Promise.prototype.setContext = function (context) { | ||
this._context = context | ||
return this | ||
} | ||
/** | ||
@@ -105,2 +111,3 @@ * Resolve this promise with a specified value | ||
var promise = new Promise(onSuccess, onFail) | ||
if (this._context) promise.setContext(this._context) | ||
@@ -168,3 +175,3 @@ if (this._child) this._child._chainPromise(promise) | ||
try { | ||
this.resolve(this._successFn(data)) | ||
this.resolve(this._successFn(data, this._context)) | ||
} catch (e) { | ||
@@ -184,3 +191,3 @@ this.reject(e) | ||
try { | ||
this.resolve(this._failFn(e)) | ||
this.resolve(this._failFn(e, this._context)) | ||
} catch (e) { | ||
@@ -187,0 +194,0 @@ this.reject(e) |
{ | ||
"name": "kew" | ||
, "description": "a lightweight promise library for node" | ||
, "version": "0.1.1" | ||
, "version": "0.1.2" | ||
, "homepage": "https://github.com/Obvious/kew" | ||
@@ -6,0 +6,0 @@ , "authors": [ |
@@ -337,2 +337,35 @@ var Q = require('../kew') | ||
}) | ||
} | ||
// test that contexts work | ||
exports.testContext = function (test) { | ||
var matches = 0 | ||
var originalContext = {name: "Jeremy"} | ||
Q.resolve("hello") | ||
.setContext(originalContext) | ||
.then(function (val, context) { | ||
test.equal(context, originalContext, "Context matches") | ||
matches++ | ||
throw new Error("now to fail") | ||
}) | ||
.fail(function (e, context) { | ||
test.equal(context, originalContext, "Context matches") | ||
matches++ | ||
return Q.all([Q.resolve("a"), Q.resolve("b")]) | ||
}) | ||
.then(function (vals, context) { | ||
test.equal(context, originalContext, "Context matches") | ||
matches++ | ||
return Q.all([Q.resolve("a"), Q.reject(new Error("no"))]) | ||
}) | ||
.fail(function (e, context) { | ||
test.equal(context, originalContext, "Context matches") | ||
matches++ | ||
return Q.all([Q.resolve("a"), Q.resolve("b")]) | ||
}) | ||
.fin(function () { | ||
test.equal(matches, 4, "Should have matched the context 4 times") | ||
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
44680
930