Commit 308876d2 authored by Tristan Cavelier's avatar Tristan Cavelier

Promise doesn't use try catch anymore

parent 07f22788
...@@ -88,11 +88,11 @@ Promise.error = function (value, onError) { ...@@ -88,11 +88,11 @@ Promise.error = function (value, onError) {
*/ */
Promise.get = function (dict, property) { Promise.get = function (dict, property) {
var p = new Promise(), solver = p.defer(); var p = new Promise(), solver = p.defer();
try { // try {
solver.resolve(dict[property]); solver.resolve(dict[property]);
} catch (e) { // } catch (e) {
solver.reject(e); // solver.reject(e);
} // }
return p; return p;
}; };
...@@ -116,12 +116,12 @@ Promise.get = function (dict, property) { ...@@ -116,12 +116,12 @@ Promise.get = function (dict, property) {
*/ */
Promise.put = function (dict, property, value) { Promise.put = function (dict, property, value) {
var p = new Promise(), solver = p.defer(); var p = new Promise(), solver = p.defer();
try { // try {
dict[property] = value; dict[property] = value;
solver.resolve(dict[property]); solver.resolve(dict[property]);
} catch (e) { // } catch (e) {
solver.reject(e); // solver.reject(e);
} // }
return p; return p;
}; };
...@@ -140,13 +140,14 @@ Promise.put = function (dict, property, value) { ...@@ -140,13 +140,14 @@ Promise.put = function (dict, property, value) {
* @return {Promise} The promise * @return {Promise} The promise
*/ */
Promise.execute = function (callback) { Promise.execute = function (callback) {
var p = new Promise(), solver = p.defer(); // var p = new Promise(), solver = p.defer();
try { // try {
Promise.when(callback(), solver.resolve, solver.reject); // Promise.when(callback(), solver.resolve, solver.reject);
} catch (e) { // } catch (e) {
solver.reject(e); // solver.reject(e);
} // }
return p; // return p;
return Promise.when(callback());
}; };
/** /**
...@@ -369,9 +370,9 @@ Promise.prototype.defer = function (callback) { ...@@ -369,9 +370,9 @@ Promise.prototype.defer = function (callback) {
setTimeout(function () { setTimeout(function () {
var i; var i;
for (i = 0; i < array.length; i += 1) { for (i = 0; i < array.length; i += 1) {
try { // try {
array[i].apply(that, that._answers); array[i].apply(that, that._answers);
} catch (ignore) {} // } catch (ignore) {}
} }
}); });
// free the memory // free the memory
...@@ -389,9 +390,9 @@ Promise.prototype.defer = function (callback) { ...@@ -389,9 +390,9 @@ Promise.prototype.defer = function (callback) {
setTimeout(function () { setTimeout(function () {
var i; var i;
for (i = 0; i < array.length; i += 1) { for (i = 0; i < array.length; i += 1) {
try { // try {
array[i].apply(that, that._answers); array[i].apply(that, that._answers);
} catch (ignore) {} // } catch (ignore) {}
} }
}); });
// free the memory // free the memory
...@@ -404,9 +405,9 @@ Promise.prototype.defer = function (callback) { ...@@ -404,9 +405,9 @@ Promise.prototype.defer = function (callback) {
if (that._onProgress) { if (that._onProgress) {
var i; var i;
for (i = 0; i < that._onProgress.length; i += 1) { for (i = 0; i < that._onProgress.length; i += 1) {
try { // try {
that._onProgress[i].apply(that, arguments); that._onProgress[i].apply(that, arguments);
} catch (ignore) {} // } catch (ignore) {}
} }
} }
} }
...@@ -448,15 +449,15 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) { ...@@ -448,15 +449,15 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) {
case "resolved": case "resolved":
if (typeof onSuccess === 'function') { if (typeof onSuccess === 'function') {
setTimeout(function () { setTimeout(function () {
try { // try {
Promise.when( Promise.when(
onSuccess.apply(that, that._answers), onSuccess.apply(that, that._answers),
resolver.resolve, resolver.resolve,
resolver.reject resolver.reject
); );
} catch (e) { // } catch (e) {
resolver.reject(e); // resolver.reject(e);
} // }
}); });
} else { } else {
setTimeout(function () { setTimeout(function () {
...@@ -467,15 +468,15 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) { ...@@ -467,15 +468,15 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) {
case "rejected": case "rejected":
if (typeof onError === 'function') { if (typeof onError === 'function') {
setTimeout(function () { setTimeout(function () {
try { // try {
Promise.when( Promise.when(
onError.apply(that, that._answers), onError.apply(that, that._answers),
resolver.reject, resolver.reject,
resolver.reject resolver.reject
); );
} catch (e) { // } catch (e) {
resolver.reject(e); // resolver.reject(e);
} // }
}); });
} else { } else {
setTimeout(function () { setTimeout(function () {
...@@ -486,15 +487,16 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) { ...@@ -486,15 +487,16 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) {
default: default:
if (typeof onSuccess === 'function') { if (typeof onSuccess === 'function') {
this._onResolve.push(function () { this._onResolve.push(function () {
try { // try {
Promise.when( Promise.when(
onSuccess.apply(that, arguments), onSuccess.apply(that, arguments),
resolver.resolve, resolver.resolve,
resolver.reject resolver.reject,
); resolver.notify
} catch (e) { );
resolver.reject(e); // } catch (e) {
} // resolver.reject(e);
// }
}); });
} else { } else {
this._onResolve.push(function () { this._onResolve.push(function () {
...@@ -503,15 +505,15 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) { ...@@ -503,15 +505,15 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) {
} }
if (typeof onError === 'function') { if (typeof onError === 'function') {
this._onReject.push(function () { this._onReject.push(function () {
try { // try {
Promise.when( Promise.when(
onError.apply(that, that._answers), onError.apply(that, that._answers),
resolver.reject, resolver.reject,
resolver.reject resolver.reject
); );
} catch (e) { // } catch (e) {
resolver.reject(e); // resolver.reject(e);
} // }
}); });
} else { } else {
this._onReject.push(function () { this._onReject.push(function () {
...@@ -520,17 +522,18 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) { ...@@ -520,17 +522,18 @@ Promise.prototype.then = function (onSuccess, onError, onProgress) {
} }
if (typeof onProgress === 'function') { if (typeof onProgress === 'function') {
this._onProgress.push(function () { this._onProgress.push(function () {
var result; var result = onProgress.apply(that, arguments);
try { // var result;
result = onProgress.apply(that, arguments); // try {
if (result === undefined) { // result = onProgress.apply(that, arguments);
resolver.notify.apply(that, arguments); if (result === undefined) {
} else {
resolver.notify(result);
}
} catch (e) {
resolver.notify.apply(that, arguments); resolver.notify.apply(that, arguments);
} else {
resolver.notify(result);
} }
// } catch (e) {
// resolver.notify.apply(that, arguments);
// }
}); });
} else { } else {
this._onProgress.push(function () { this._onProgress.push(function () {
...@@ -595,9 +598,9 @@ Promise.prototype.done = function (callback) { ...@@ -595,9 +598,9 @@ Promise.prototype.done = function (callback) {
switch (this._state) { switch (this._state) {
case "resolved": case "resolved":
setTimeout(function () { setTimeout(function () {
try { // try {
callback.apply(that, that._answers); callback.apply(that, that._answers);
} catch (ignore) {} // } catch (ignore) {}
}); });
break; break;
case "rejected": case "rejected":
...@@ -630,9 +633,9 @@ Promise.prototype.fail = function (callback) { ...@@ -630,9 +633,9 @@ Promise.prototype.fail = function (callback) {
switch (this._state) { switch (this._state) {
case "rejected": case "rejected":
setTimeout(function () { setTimeout(function () {
try { // try {
callback.apply(that, that._answers); callback.apply(that, that._answers);
} catch (ignore) {} // } catch (ignore) {}
}); });
break; break;
case "resolved": case "resolved":
...@@ -695,9 +698,9 @@ Promise.prototype.always = function (callback) { ...@@ -695,9 +698,9 @@ Promise.prototype.always = function (callback) {
case "resolved": case "resolved":
case "rejected": case "rejected":
setTimeout(function () { setTimeout(function () {
try { // try {
callback.apply(that, that._answers); callback.apply(that, that._answers);
} catch (ignore) {} // } catch (ignore) {}
}); });
break; break;
default: default:
......
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