Skip to content

Commit

Permalink
Promise-Aplus v2.0.3 WIP #1
Browse files Browse the repository at this point in the history
Got 30~ tests passing in the 2.3.3 test suite
  • Loading branch information
Caitlin Potter committed Dec 17, 2013
1 parent cbb3ce2 commit d917880
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
12 changes: 3 additions & 9 deletions lib/promises-aplus/promises-aplus-test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ var isFunction = function isFunction(value){return typeof value == 'function';}

var $q = qFactory(process.nextTick, function noopExceptionHandler() {});

exports.fulfilled = $q.resolve;
exports.resolved = $q.resolve;
exports.rejected = $q.reject;
exports.pending = function () {
var deferred = $q.defer();

return {
promise: deferred.promise,
fulfill: deferred.resolve,
reject: deferred.reject
};
exports.deferred = function () {
return $q.defer();
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"grunt-parallel": "~0.3.1",
"grunt-ddescribe-iit": "~0.0.1",
"grunt-merge-conflict": "~0.0.1",
"promises-aplus-tests": "~1.3.2",
"promises-aplus-tests": "~2.0.3",
"grunt-shell": "~0.4.0",
"semver": "~2.1.0",
"lodash": "~2.1.0",
Expand Down
25 changes: 21 additions & 4 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ function qFactory(nextTick, exceptionHandler) {
deferred = {

resolve: function(val) {
if (val === deferred) {
throw new TypeError();
}
if (pending) {
var callbacks = pending;
pending = undefined;
Expand All @@ -214,7 +217,9 @@ function qFactory(nextTick, exceptionHandler) {
var callback;
for (var i = 0, ii = callbacks.length; i < ii; i++) {
callback = callbacks[i];
value.then(callback[0], callback[1], callback[2]);
if (typeof value.then === 'function') {
value.then.call(value, callback[0], callback[1], callback[2]);
}
}
});
}
Expand All @@ -223,6 +228,9 @@ function qFactory(nextTick, exceptionHandler) {


reject: function(reason) {
if (reason === deferred || reason === deferred.promise) {
throw new TypeError();
}
deferred.resolve(reject(reason));
},

Expand Down Expand Up @@ -277,7 +285,7 @@ function qFactory(nextTick, exceptionHandler) {
if (pending) {
pending.push([wrappedCallback, wrappedErrback, wrappedProgressback]);
} else {
value.then(wrappedCallback, wrappedErrback, wrappedProgressback);
value.then.call(value, wrappedCallback, wrappedErrback, wrappedProgressback);
}

return result.promise;
Expand Down Expand Up @@ -331,7 +339,16 @@ function qFactory(nextTick, exceptionHandler) {


var ref = function(value) {
if (value && isFunction(value.then)) return value;
if (value && isFunction(value) || typeof value === 'object') {
var then;
try {
if (then = value.then && typeof then === 'function') {
return value;
}
} catch (e) {
return reject(e);
}
}
return {
then: function(callback) {
var result = defer();
Expand All @@ -343,7 +360,6 @@ function qFactory(nextTick, exceptionHandler) {
};
};


/**
* @ngdoc
* @name ng.$q#reject
Expand Down Expand Up @@ -380,6 +396,7 @@ function qFactory(nextTick, exceptionHandler) {
* @returns {Promise} Returns a promise that was already resolved as rejected with the `reason`.
*/
var reject = function(reason) {
console.log('reject', reason);
return {
then: function(callback, errback) {
var result = defer();
Expand Down

0 comments on commit d917880

Please sign in to comment.