Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2248)

Unified Diff: chrome/common/extensions/docs/server2/future.py

Issue 417163004: Docserver: Update Future.Then() to be more Promise-like (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add tests Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/future.py
diff --git a/chrome/common/extensions/docs/server2/future.py b/chrome/common/extensions/docs/server2/future.py
index 4b09073d4c25c0812fcbb9494dd731333f4cedf9..51c284235cc7706d4f9c9df0f2740cdf18d409b0 100644
--- a/chrome/common/extensions/docs/server2/future.py
+++ b/chrome/common/extensions/docs/server2/future.py
@@ -7,6 +7,10 @@ import sys
_no_value = object()
+def _DefaultErrorHandler(error):
+ raise error
+
+
def All(futures, except_pass=None):
'''Creates a Future which returns a list of results from each Future in
|futures|.
@@ -61,12 +65,17 @@ class Future(object):
self._exc_info is None):
raise ValueError('Must have either a value, error, or callback.')
- def Then(self, callback):
+ def Then(self, callback, error_handler=_DefaultErrorHandler):
'''Creates and returns a future that runs |callback| on the value of this
- future.
+ future, or runs optional |error_handler| if resolving this future results in
+ an exception.
'''
def then():
- return callback(self.Get())
+ try:
+ val = self.Get()
+ except Exception as e:
+ return error_handler(e)
+ return callback(val)
return Future(callback=then)
def Get(self):
« no previous file with comments | « chrome/common/extensions/docs/server2/file_system.py ('k') | chrome/common/extensions/docs/server2/future_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698