Commit 5bd85804 authored by Teddy Zeenny's avatar Teddy Zeenny

Make rsvp promises/A+ spec-1.1 compliant

parent a7f93c16
...@@ -61,12 +61,8 @@ var invokeCallback = function(type, promise, callback, event) { ...@@ -61,12 +61,8 @@ var invokeCallback = function(type, promise, callback, event) {
succeeded = true; succeeded = true;
} }
if (objectOrFunction(value) && isFunction(value.then)) { if (handleThenable(promise, value)) {
value.then(function(value) { return;
resolve(promise, value);
}, function(error) {
reject(promise, error);
});
} else if (hasCallback && succeeded) { } else if (hasCallback && succeeded) {
resolve(promise, value); resolve(promise, value);
} else if (failed) { } else if (failed) {
...@@ -111,11 +107,27 @@ Promise.prototype = { ...@@ -111,11 +107,27 @@ Promise.prototype = {
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
function resolve(promise, value) { function resolve(promise, value) {
if (promise === value) { if (promise === value) {
fulfill(promise, value); fulfill(promise, value);
} else if (objectOrFunction(value) && isFunction(value.then)) { } else if (!handleThenable(promise, value)) {
value.then(function(val) { fulfill(promise, value);
}
}
function handleThenable(promise, value) {
var then = null;
if (objectOrFunction(value)) {
try {
then = value.then;
} catch(e) {
reject(promise, e);
return true;
}
if (isFunction(then)) {
try {
then.call(value, function(val) {
if (value !== val) { if (value !== val) {
resolve(promise, val); resolve(promise, val);
} else { } else {
...@@ -124,9 +136,14 @@ function resolve(promise, value) { ...@@ -124,9 +136,14 @@ function resolve(promise, value) {
}, function(val) { }, function(val) {
reject(promise, val); reject(promise, val);
}); });
} else { } catch (e) {
fulfill(promise, value); reject(promise, e);
}
return true;
} }
}
return false;
} }
function fulfill(promise, value) { function fulfill(promise, value) {
......
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