From 632c4bd2038b7882b5f2608436c58b9016bd77f6 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Tue, 4 Dec 2012 15:59:48 -0800 Subject: [PATCH 1/6] Remove require calls in comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These trip Montage Require’s heuristic dependency analysis. --- q.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/q.js b/q.js index 3c22c770..17d52d3d 100644 --- a/q.js +++ b/q.js @@ -1380,8 +1380,7 @@ function delay(promise, timeout) { * Passes a continuation to a Node function, which is called with the given * arguments provided as an array, and returns a promise. * - * var readFile = require("fs").readFile; - * Q.nfapply(readFile, [__filename]) + * Q.nfapply(FS.readFile, [__filename]) * .then(function (content) { * }) * @@ -1400,8 +1399,7 @@ function nfapply(callback, args) { * Passes a continuation to a Node function, which is called with the given * arguments provided individually, and returns a promise. * - * var readFile = require("fs").readFile; - * Q.nfcall(readFile, __filename) + * Q.nfcall(FS.readFile, __filename) * .then(function (content) { * }) * @@ -1442,7 +1440,6 @@ function nfbind(callback/*, ...args */) { * Passes a continuation to a Node function, which is called with a given * `this` value and arguments provided as an array, and returns a promise. * - * var FS = (require)("fs"); * Q.napply(FS.readFile, FS, [__filename]) * .then(function (content) { * }) @@ -1457,7 +1454,6 @@ function napply(callback, thisp, args) { * Passes a continuation to a Node function, which is called with a given * `this` value and arguments provided individually, and returns a promise. * - * var FS = (require)("fs"); * Q.ncall(FS.readFile, FS, __filename) * .then(function (content) { * }) From 20d7f7a3fed9981f257aa13acf23158b028967e5 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Fri, 21 Dec 2012 12:59:49 -0500 Subject: [PATCH 2/6] Treat foreign promises as unresolved. Should allow Q.all to coerce foreign promises instead of treating them as fulfilled values. Should fix #154. No spec. --- q.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/q.js b/q.js index 17d52d3d..ef070a0a 100644 --- a/q.js +++ b/q.js @@ -576,6 +576,15 @@ function isPromise(object) { return object && typeof object.promiseSend === "function"; } +/** + * @returns whether the given object can be coerced to a promise. + * Otherwise it is a fulfilled value. + */ +exports.isPromiseAlike = isPromiseAlike; +function isPromiseAlike(object) { + return object && typeof object.then === "function"; +} + /** * @returns whether the given object is a resolved promise. */ @@ -590,7 +599,7 @@ function isResolved(object) { */ exports.isFulfilled = isFulfilled; function isFulfilled(object) { - return !isPromise(valueOf(object)); + return !isPromiseAlike(valueOf(object)); } /** @@ -676,7 +685,7 @@ function resolve(object) { // implementations on primordial prototypes are harmless. object = valueOf(object); // assimilate thenables, CommonJS/Promises/A - if (object && typeof object.then === "function") { + if (isPromiseAlike(object)) { var deferred = defer(); object.then(deferred.resolve, deferred.reject, deferred.notify); return deferred.promise; From 1fe1aa824424bad01d1aa5e0a32d62c72e341b22 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Sat, 29 Dec 2012 17:28:18 -0500 Subject: [PATCH 3/6] Add spec for #154/#155/#156. --- spec/q-spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/q-spec.js b/spec/q-spec.js index 36c7e7aa..529fe395 100644 --- a/spec/q-spec.js +++ b/spec/q-spec.js @@ -951,6 +951,16 @@ describe("all", function () { .timeout(1000); }); + it("resolves foreign promises", function () { + var normal = Q.resolve(1); + var foreign = { then: function (f) { f(2); } }; + + return Q.all([normal, foreign]) + .then(function (result) { + expect(result).toEqual([1, 2]); + }); + }); + }); describe("allResolved", function () { From 5bac9134a05d5068dcf00d2709bec4a53ce8e8de Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Sat, 29 Dec 2012 17:34:54 -0500 Subject: [PATCH 4/6] Add Promises/A+ tests (currently failing). --- package.json | 5 +++-- spec/aplus-adapter.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 spec/aplus-adapter.js diff --git a/package.json b/package.json index 335d676f..80855aed 100644 --- a/package.json +++ b/package.json @@ -43,10 +43,11 @@ "jshint": ">=0.9.1", "cover": "*", "jasmine-node": "*", - "opener": "*" + "opener": "*", + "promises-aplus-tests": "~1.0" }, "scripts": { - "test": "jasmine-node spec", + "test": "jasmine-node spec && promises-aplus-tests spec/aplus-adapter", "test-browser": "opener spec/q-spec.html", "lint": "jshint q.js", "cover": "cover run node_modules/jasmine-node/bin/jasmine-node spec && cover report html && opener cover_html/index.html" diff --git a/spec/aplus-adapter.js b/spec/aplus-adapter.js new file mode 100644 index 00000000..f2c659dc --- /dev/null +++ b/spec/aplus-adapter.js @@ -0,0 +1,15 @@ +"use strict"; + +var Q = require("../q"); + +exports.fulfilled = Q.resolve; +exports.rejected = Q.reject; +exports.pending = function () { + var deferred = Q.defer(); + + return { + promise: deferred.promise, + fulfill: deferred.resolve, + reject: deferred.reject + }; +}; From 4b93d1bcac9b3406ac2510ce103d0f7a8848d192 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Sat, 29 Dec 2012 17:35:39 -0500 Subject: [PATCH 5/6] Allow falsy values as rejection reasons. Closes #157. --- q.js | 1 - 1 file changed, 1 deletion(-) diff --git a/q.js b/q.js index ef070a0a..8908dc0f 100644 --- a/q.js +++ b/q.js @@ -636,7 +636,6 @@ function displayErrors() { */ exports.reject = reject; function reject(exception) { - exception = exception || new Error(); var rejection = makePromise({ "when": function (rejected) { // note that the error has been handled From 76afd7b7abaad30cbfee394c72227d9dc06154e6 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Sat, 29 Dec 2012 17:37:12 -0500 Subject: [PATCH 6/6] Ignore truthy non-functions passed to `then`. Fixes #158. --- q.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/q.js b/q.js index 8908dc0f..974b4c33 100644 --- a/q.js +++ b/q.js @@ -827,14 +827,14 @@ function when(value, fulfilled, rejected, progressed) { function _fulfilled(value) { try { - return fulfilled ? fulfilled(value) : value; + return typeof fulfilled === "function" ? fulfilled(value) : value; } catch (exception) { return reject(exception); } } function _rejected(exception) { - if (rejected) { + if (typeof rejected === "function") { makeStackTraceLong(exception, resolvedValue); try { return rejected(exception); @@ -846,7 +846,7 @@ function when(value, fulfilled, rejected, progressed) { } function _progressed(value) { - return progressed ? progressed(value) : value; + return typeof progressed === "function" ? progressed(value) : value; } var resolvedValue = resolve(value);