OLD | NEW |
---|---|
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 Loading... | |
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): |
Yoyo Zhou
2014/08/01 22:03:19
This should come with tests.
P.S. You should make
ahernandez
2014/08/04 18:51:19
The other changes do use the new changes from futu
| |
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. Optional |error_handler| can be passed for custom error handling. |
67 ''' | 71 ''' |
68 def then(): | 72 def then(): |
69 return callback(self.Get()) | 73 try: |
74 return callback(self.Get()) | |
Yoyo Zhou
2014/08/01 22:03:19
If I'm reading the Promise description correctly,
| |
75 except Exception as e: | |
76 error_handler(e) | |
70 return Future(callback=then) | 77 return Future(callback=then) |
71 | 78 |
72 def Get(self): | 79 def Get(self): |
73 '''Gets the stored value, error, or callback contents. | 80 '''Gets the stored value, error, or callback contents. |
74 ''' | 81 ''' |
75 if self._value is not _no_value: | 82 if self._value is not _no_value: |
76 return self._value | 83 return self._value |
77 if self._exc_info is not None: | 84 if self._exc_info is not None: |
78 self._Raise() | 85 self._Raise() |
79 try: | 86 try: |
80 self._value = self._callback() | 87 self._value = self._callback() |
81 return self._value | 88 return self._value |
82 except: | 89 except: |
83 self._exc_info = sys.exc_info() | 90 self._exc_info = sys.exc_info() |
84 self._Raise() | 91 self._Raise() |
85 | 92 |
86 def _Raise(self): | 93 def _Raise(self): |
87 exc_info = self._exc_info | 94 exc_info = self._exc_info |
88 raise exc_info[0], exc_info[1], exc_info[2] | 95 raise exc_info[0], exc_info[1], exc_info[2] |
OLD | NEW |