OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """ | 5 """ |
6 Promise used by the python bindings. | 6 Promise used by the python bindings. |
7 | 7 |
8 The API is following the ECMAScript 6 API for promises. | 8 The API is following the ECMAScript 6 API for promises. |
9 """ | 9 """ |
10 | 10 |
11 import sys | |
12 | |
13 | 11 |
14 class Promise(object): | 12 class Promise(object): |
15 """The promise object.""" | 13 """The promise object.""" |
16 | 14 |
17 STATE_PENDING = 0 | 15 STATE_PENDING = 0 |
18 STATE_FULLFILLED = 1 | 16 STATE_FULLFILLED = 1 |
19 STATE_REJECTED = 2 | 17 STATE_REJECTED = 2 |
20 STATE_BOUND = 3 | 18 STATE_BOUND = 3 |
21 | 19 |
22 def __init__(self, generator_function): | 20 def __init__(self, generator_function): |
23 """ | 21 """ |
24 Constructor. | 22 Constructor. |
25 | 23 |
26 Args: | 24 Args: |
27 generator_function: A function taking 2 arguments: resolve and reject. | 25 generator_function: A function taking 2 arguments: resolve and reject. |
28 When |resolve| is called, the promise is fullfilled with the given value. | 26 When |resolve| is called, the promise is fullfilled with the given value. |
29 When |reject| is called, the promise is rejected with the given value. | 27 When |reject| is called, the promise is rejected with the given value. |
30 A promise can only be resolved or rejected once, all following calls will | 28 A promise can only be resolved or rejected once, all following calls will |
31 have no effect. | 29 have no effect. |
32 """ | 30 """ |
33 self._onCatched = [] | 31 self._onCatched = [] |
34 self._onFulfilled = [] | 32 self._onFulfilled = [] |
35 self._onRejected = [] | 33 self._onRejected = [] |
36 self._state = Promise.STATE_PENDING | 34 self._state = Promise.STATE_PENDING |
37 self._result = None | 35 self._result = None |
38 try: | 36 if generator_function: |
39 generator_function(self._Resolve, self._Reject) | 37 generator_function(self._Resolve, self._Reject) |
40 except Exception as e: | |
41 # Adding traceback similarly to python 3.0 (pep-3134) | |
42 e.__traceback__ = sys.exc_info()[2] | |
43 self._Reject(e) | |
44 | 38 |
45 @staticmethod | 39 @staticmethod |
46 def Resolve(value): | 40 def Resolve(value): |
47 """ | 41 """ |
48 If value is a promise, make a promise that have the same behavior as value, | 42 If value is a promise, make a promise that have the same behavior as value, |
49 otherwise make a promise that fulfills to value. | 43 otherwise make a promise that fulfills to value. |
50 """ | 44 """ |
51 if isinstance(value, Promise): | 45 if isinstance(value, Promise): |
52 return value | 46 return value |
53 return Promise(lambda x, y: x(value)) | 47 return Promise(lambda x, y: x(value)) |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 177 |
184 | 178 |
185 def _Delegate(resolve, reject, action): | 179 def _Delegate(resolve, reject, action): |
186 def _Run(x): | 180 def _Run(x): |
187 try: | 181 try: |
188 if action: | 182 if action: |
189 resolve(action(x)) | 183 resolve(action(x)) |
190 else: | 184 else: |
191 resolve(x) | 185 resolve(x) |
192 except Exception as e: | 186 except Exception as e: |
193 # Adding traceback similarly to python 3.0 (pep-3134) | |
194 e.__traceback__ = sys.exc_info()[2] | |
195 reject(e) | 187 reject(e) |
196 return _Run | 188 return _Run |
OLD | NEW |