Commit 7052bff8 authored by Stefan Penner's avatar Stefan Penner

prevent multiple promise resolution

This was handled for some code paths but not all.
parent db474da1
......@@ -10,8 +10,7 @@ function isFunction(x){
}
var Promise = function(resolver) {
var promise = this,
resolved = false;
var promise = this;
if (typeof resolver !== 'function') {
throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor');
......@@ -22,14 +21,10 @@ var Promise = function(resolver) {
}
var resolvePromise = function(value) {
if (resolved) { return; }
resolved = true;
resolve(promise, value);
};
var rejectPromise = function(value) {
if (resolved) { return; }
resolved = true;
reject(promise, value);
};
......@@ -88,7 +83,9 @@ var invokeCallback = function(type, promise, callback, event) {
Promise.prototype = {
constructor: Promise,
isFulfilled: undefined,
isRejected: undefined,
isResolved: undefined,
then: function(done, fail) {
this.off('error', onerror);
......@@ -158,6 +155,9 @@ function handleThenable(promise, value) {
}
function fulfill(promise, value) {
if (promise.isResolved){ return; }
promise.isResolved = true;
config.async(function() {
promise.trigger('promise:resolved', { detail: value });
promise.isFulfilled = true;
......@@ -166,6 +166,9 @@ function fulfill(promise, value) {
}
function reject(promise, value) {
if (promise.isResolved){ return; }
promise.isResolved = true;
config.async(function() {
promise.trigger('promise:failed', { detail: value });
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