Skip to content

Commit

Permalink
Add nend
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Oct 8, 2012
1 parent facbb77 commit 7d86d9f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion 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/)",
Expand Down
26 changes: 25 additions & 1 deletion q.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {

This comment has been minimized.

Copy link
@domenic

domenic Oct 11, 2012

Collaborator

Why nextTick? The then should take care of it.

This comment has been minimized.

Copy link
@kriskowal

kriskowal Oct 11, 2012

Author Owner

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

This comment has been minimized.

Copy link
@domenic

domenic Oct 11, 2012

Collaborator

Right, nice.

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.
Expand Down
26 changes: 26 additions & 0 deletions spec/q-spec.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 7d86d9f

Please sign in to comment.