Commit fb1a1168 authored by Tristan Cavelier's avatar Tristan Cavelier

Promise doesn't use try catch anymore

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