Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add nend

  • Loading branch information...
commit 7d86d9f19a21643d4323ca9e70d148773198dfc7 1 parent facbb77
@kriskowal authored
Showing with 56 additions and 2 deletions.
  1. +4 −0 CHANGES.md
  2. +1 −1  package.json
  3. +25 −1 q.js
  4. +26 −0 spec/q-spec.js
View
4 CHANGES.md
@@ -14,6 +14,10 @@
``invoke``.
- WARNING: The undocumented ``view`` and ``viewInfo`` will be removed.
+## 0.8.9
+
+ - Add `nend`
+
## 0.8.7
- Support [Montage Require](http://github.com/kriskowal/mr)
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "q",
- "version": "0.8.8",
+ "version": "0.8.9",
"description": "A library for promises (CommonJS/Promises/A,B,D)",
"homepage": "http://github.com/kriskowal/q/",
"author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)",
View
26 q.js
@@ -631,7 +631,10 @@ array_reduce(
"all", "allResolved",
"view", "viewInfo",
"timeout", "delay",
- "catch", "finally", "fail", "fin", "progress", "end"
+ "catch", "finally", "fail", "fin", "progress", "end",
+ "ncall", "napply", "nbind",
+ "npost", "ninvoke",
+ "nend"
],
function (undefined, name) {
makePromise.prototype[name] = function () {
@@ -1572,6 +1575,27 @@ function ninvoke(object, name /*, ...args*/) {
return napply(object[name], object, args);
}
+exports.nend = nend;
+function nend(promise, nodeback) {
+ if (nodeback) {
+ var deferred = defer();
+ promise.then(function (value) {
+ nextTick(function () {
@domenic Collaborator
domenic added a note

Why nextTick? The then should take care of it.

@kriskowal Owner

nextTick escapes then’s try/catch, so exceptions get handled in the node fashion.

@domenic Collaborator
domenic added a note

Right, nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ deferred.resolve();
+ nodeback(null, value);
+ });
+ }, function (error) {
+ nextTick(function () {
+ deferred.resolve();
+ nodeback(error);
+ });
+ });
+ return deferred.promise;
+ } else {
+ return promise;
+ }
+}
+
defend(exports);
// All code before this point will be filtered from stack traces.
View
26 spec/q-spec.js
@@ -1411,6 +1411,32 @@ describe("node support", function () {
});
+ describe("nend", function () {
+
+ it("calls back with a resolution", function () {
+ var spy = jasmine.createSpy();
+ return Q.resolve(10).nend(spy)
+ .then(function () {
+ expect(spy.argsForCall).toEqual([[null, 10]]);
+ });
+ });
+
+ it("calls back with an error", function () {
+ var spy = jasmine.createSpy();
+ return Q.reject(10).nend(spy)
+ .then(function () {
+ expect(spy.argsForCall).toEqual([[10]]);
+ });
+ });
+
+ it("forwards a promise", function () {
+ return Q.resolve(10).nend().then(function (ten) {
+ expect(ten).toBe(10);
+ });
+ });
+
+ });
+
});
describe("decorator functions", function () {
Please sign in to comment.
Something went wrong with that request. Please try again.