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

Side by Side 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 unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import sys 5 import sys
6 6
7 _no_value = object() 7 _no_value = object()
8 8
9 9
10 def _DefaultErrorHandler(error):
11 raise error
12
13
10 def All(futures, except_pass=None): 14 def All(futures, except_pass=None):
11 '''Creates a Future which returns a list of results from each Future in 15 '''Creates a Future which returns a list of results from each Future in
12 |futures|. 16 |futures|.
13 17
14 If any Future raises an error other than those in |except_pass| the returned 18 If any Future raises an error other than those in |except_pass| the returned
15 Future will raise as well. 19 Future will raise as well.
16 ''' 20 '''
17 def resolve(): 21 def resolve():
18 resolved = [] 22 resolved = []
19 for f in futures: 23 for f in futures:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 ''' 58 '''
55 def __init__(self, value=_no_value, callback=None, exc_info=None): 59 def __init__(self, value=_no_value, callback=None, exc_info=None):
56 self._value = value 60 self._value = value
57 self._callback = callback 61 self._callback = callback
58 self._exc_info = exc_info 62 self._exc_info = exc_info
59 if (self._value is _no_value and 63 if (self._value is _no_value and
60 self._callback is None and 64 self._callback is None and
61 self._exc_info is None): 65 self._exc_info is None):
62 raise ValueError('Must have either a value, error, or callback.') 66 raise ValueError('Must have either a value, error, or callback.')
63 67
64 def Then(self, callback): 68 def Then(self, callback, error_handler=_DefaultErrorHandler):
65 '''Creates and returns a future that runs |callback| on the value of this 69 '''Creates and returns a future that runs |callback| on the value of this
66 future. 70 future, or runs optional |error_handler| if resolving this future results in
71 an exception.
67 ''' 72 '''
68 def then(): 73 def then():
69 return callback(self.Get()) 74 try:
75 val = self.Get()
76 except Exception as e:
77 return error_handler(e)
78 return callback(val)
70 return Future(callback=then) 79 return Future(callback=then)
71 80
72 def Get(self): 81 def Get(self):
73 '''Gets the stored value, error, or callback contents. 82 '''Gets the stored value, error, or callback contents.
74 ''' 83 '''
75 if self._value is not _no_value: 84 if self._value is not _no_value:
76 return self._value 85 return self._value
77 if self._exc_info is not None: 86 if self._exc_info is not None:
78 self._Raise() 87 self._Raise()
79 try: 88 try:
80 self._value = self._callback() 89 self._value = self._callback()
81 return self._value 90 return self._value
82 except: 91 except:
83 self._exc_info = sys.exc_info() 92 self._exc_info = sys.exc_info()
84 self._Raise() 93 self._Raise()
85 94
86 def _Raise(self): 95 def _Raise(self):
87 exc_info = self._exc_info 96 exc_info = self._exc_info
88 raise exc_info[0], exc_info[1], exc_info[2] 97 raise exc_info[0], exc_info[1], exc_info[2]
OLDNEW
« 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