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

Side by Side Diff: chrome/common/extensions/docs/server2/future.py

Issue 438403002: Conver APIListDataSource to use Future.Then() rather than Get(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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): 10 def _DefaultErrorHandler(error):
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 self._exc_info = exc_info 62 self._exc_info = exc_info
63 if (self._value is _no_value and 63 if (self._value is _no_value and
64 self._callback is None and 64 self._callback is None and
65 self._exc_info is None): 65 self._exc_info is None):
66 raise ValueError('Must have either a value, error, or callback.') 66 raise ValueError('Must have either a value, error, or callback.')
67 67
68 def Then(self, callback, error_handler=_DefaultErrorHandler): 68 def Then(self, callback, error_handler=_DefaultErrorHandler):
69 '''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
70 future, or runs optional |error_handler| if resolving this future results in 70 future, or runs optional |error_handler| if resolving this future results in
71 an exception. 71 an exception.
72
73 If |callback| returns a Future then Then() will resolve to the same value
Yoyo Zhou 2014/08/05 21:52:36 These 2 sentences are confusing next to each other
not at google - send to devlin 2014/08/05 23:00:54 This is tricky to explain. I had another go.
74 of that Future. Otherwise, Then() will resolve to that value.
75
76 For example,
77
78 def fortytwo():
79 return 42
80 def inc(x):
81 return x + 1
82 def inc_future(x):
83 return Future(value=x + 1)
84
85 fortywho().Then(inc).Get() ==> 43
86 fortywho().Then(inc_future).Get() ==> 43
72 ''' 87 '''
73 def then(): 88 def then():
89 val = None
74 try: 90 try:
75 val = self.Get() 91 val = self.Get()
76 except Exception as e: 92 except Exception as e:
77 return error_handler(e) 93 val = error_handler(e)
78 return callback(val) 94 else:
95 val = callback(val)
96 return val.Get() if isinstance(val, Future) else val
79 return Future(callback=then) 97 return Future(callback=then)
80 98
81 def Get(self): 99 def Get(self):
82 '''Gets the stored value, error, or callback contents. 100 '''Gets the stored value, error, or callback contents.
83 ''' 101 '''
84 if self._value is not _no_value: 102 if self._value is not _no_value:
85 return self._value 103 return self._value
86 if self._exc_info is not None: 104 if self._exc_info is not None:
87 self._Raise() 105 self._Raise()
88 try: 106 try:
89 self._value = self._callback() 107 self._value = self._callback()
90 return self._value 108 return self._value
91 except: 109 except:
92 self._exc_info = sys.exc_info() 110 self._exc_info = sys.exc_info()
93 self._Raise() 111 self._Raise()
94 112
95 def _Raise(self): 113 def _Raise(self):
96 exc_info = self._exc_info 114 exc_info = self._exc_info
97 raise exc_info[0], exc_info[1], exc_info[2] 115 raise exc_info[0], exc_info[1], exc_info[2]
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698