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,24 +107,45 @@ Promise.prototype = { ...@@ -111,24 +107,45 @@ 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) {
if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} else {
fulfill(promise, value); 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) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} catch (e) {
reject(promise, e);
}
return true;
}
}
return false;
}
function fulfill(promise, value) { function fulfill(promise, value) {
config.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: 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