Experimental approach to cancelling unnecessary promises

If a view or other resources are destroyed while a promise is being
resolved, subsequent code can fail. This is generally harmless, but it
results in unnecessary errors being logged to the console.

To address this, promises can use a new function,
Zotero.Promise.check(), to test whether a value is truthy or 0 and
automatically throw a specific error that's ignored by the unhandled
rejection handler if not.

Example usage:

getAsync().tap(() => Zotero.Promise.check(this.win));

If this.win is cleaned up while getAsync() is being resolved, subsequent
lines won't be run, and nothing will be logged to the console.
This commit is contained in:
Dan Stillman 2015-05-23 03:04:32 -04:00
parent 3fc09add3a
commit 6b87c641d9
3 changed files with 44 additions and 10 deletions

View file

@ -91,12 +91,16 @@
Promise = e();
// TEMP: Only turn on if debug logging enabled?
Promise.longStackTraces();
Promise.onPossiblyUnhandledRejection(function(error) {
Promise.onPossiblyUnhandledRejection(function (e, promise) {
if (e.name == 'ZoteroPromiseInterrupt') {
return;
}
// Ignore some errors during tests
if (error.message && error.message.indexOf(' -- ignore') != -1) return;
if (e.message && e.message.indexOf(' -- ignore') != -1) return;
self.debug('Unhandled rejection:\n\n' + error.stack);
throw error;
self.debug('Unhandled rejection:\n\n' + e.stack);
throw e;
});
return;