Commit 54e7bf00 authored by Stefan Penner's avatar Stefan Penner

Merge pull request #93 from stefanpenner/master

some a+ 1.1 spec fixes.
parents 53d478fb 277eb5ea
...@@ -10,7 +10,8 @@ function isFunction(x){ ...@@ -10,7 +10,8 @@ function isFunction(x){
} }
var Promise = function(resolver) { var Promise = function(resolver) {
var promise = this; var promise = this,
resolved = false;
if (typeof resolver !== 'function') { if (typeof resolver !== 'function') {
throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor'); throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor');
...@@ -21,10 +22,14 @@ var Promise = function(resolver) { ...@@ -21,10 +22,14 @@ var Promise = function(resolver) {
} }
var resolvePromise = function(value) { var resolvePromise = function(value) {
if (resolved) { return; }
resolved = true;
resolve(promise, value); resolve(promise, value);
}; };
var rejectPromise = function(value) { var rejectPromise = function(value) {
if (resolved) { return; }
resolved = true;
reject(promise, value); reject(promise, value);
}; };
...@@ -83,9 +88,12 @@ var invokeCallback = function(type, promise, callback, event) { ...@@ -83,9 +88,12 @@ var invokeCallback = function(type, promise, callback, event) {
Promise.prototype = { Promise.prototype = {
constructor: Promise, constructor: Promise,
isFulfilled: undefined,
isRejected: undefined, isRejected: undefined,
isResolved: undefined, isFulfilled: undefined,
rejectedReason: undefined,
fulfillmentValue: undefined,
then: function(done, fail) { then: function(done, fail) {
this.off('error', onerror); this.off('error', onerror);
...@@ -126,20 +134,31 @@ function resolve(promise, value) { ...@@ -126,20 +134,31 @@ function resolve(promise, value) {
} }
function handleThenable(promise, value) { function handleThenable(promise, value) {
var then = null; var then = null,
resolved;
try { try {
if (promise === value) {
throw new TypeError("A promises callback cannot return that same promise.");
}
if (objectOrFunction(value)) { if (objectOrFunction(value)) {
then = value.then; then = value.then;
if (isFunction(then)) { if (isFunction(then)) {
then.call(value, function(val) { then.call(value, function(val) {
if (resolved) { return true; }
resolved = true;
if (value !== val) { if (value !== val) {
resolve(promise, val); resolve(promise, val);
} else { } else {
fulfill(promise, val); fulfill(promise, val);
} }
}, function(val) { }, function(val) {
if (resolved) { return true; }
resolved = true;
reject(promise, val); reject(promise, val);
}); });
...@@ -155,9 +174,6 @@ function handleThenable(promise, value) { ...@@ -155,9 +174,6 @@ function handleThenable(promise, value) {
} }
function fulfill(promise, value) { function fulfill(promise, value) {
if (promise.isResolved){ return; }
promise.isResolved = true;
config.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isFulfilled = true; promise.isFulfilled = true;
...@@ -166,9 +182,6 @@ function fulfill(promise, value) { ...@@ -166,9 +182,6 @@ function fulfill(promise, value) {
} }
function reject(promise, value) { function reject(promise, value) {
if (promise.isResolved){ return; }
promise.isResolved = true;
config.async(function() { config.async(function() {
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment