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

Side by Side Diff: mojo/public/python/mojo/bindings/promise.py

Issue 695583002: Update mojo sdk to rev e083961bf11fd0c94d40be8853761da529b6d444 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Patch in https://codereview.chromium.org/692823003 Created 6 years, 1 month 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 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
11 13
12 class Promise(object): 14 class Promise(object):
13 """The promise object.""" 15 """The promise object."""
14 16
15 STATE_PENDING = 0 17 STATE_PENDING = 0
16 STATE_FULLFILLED = 1 18 STATE_FULLFILLED = 1
17 STATE_REJECTED = 2 19 STATE_REJECTED = 2
18 STATE_BOUND = 3 20 STATE_BOUND = 3
19 21
20 def __init__(self, generator_function): 22 def __init__(self, generator_function):
21 """ 23 """
22 Constructor. 24 Constructor.
23 25
24 Args: 26 Args:
25 generator_function: A function taking 2 arguments: resolve and reject. 27 generator_function: A function taking 2 arguments: resolve and reject.
26 When |resolve| is called, the promise is fullfilled with the given value. 28 When |resolve| is called, the promise is fullfilled with the given value.
27 When |reject| is called, the promise is rejected with the given value. 29 When |reject| is called, the promise is rejected with the given value.
28 A promise can only be resolved or rejected once, all following calls will 30 A promise can only be resolved or rejected once, all following calls will
29 have no effect. 31 have no effect.
30 """ 32 """
31 self._onCatched = [] 33 self._onCatched = []
32 self._onFulfilled = [] 34 self._onFulfilled = []
33 self._onRejected = [] 35 self._onRejected = []
34 self._state = Promise.STATE_PENDING 36 self._state = Promise.STATE_PENDING
35 self._result = None 37 self._result = None
36 if generator_function: 38 try:
37 generator_function(self._Resolve, self._Reject) 39 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)
38 44
39 @staticmethod 45 @staticmethod
40 def Resolve(value): 46 def Resolve(value):
41 """ 47 """
42 If value is a promise, make a promise that have the same behavior as value, 48 If value is a promise, make a promise that have the same behavior as value,
43 otherwise make a promise that fulfills to value. 49 otherwise make a promise that fulfills to value.
44 """ 50 """
45 if isinstance(value, Promise): 51 if isinstance(value, Promise):
46 return value 52 return value
47 return Promise(lambda x, y: x(value)) 53 return Promise(lambda x, y: x(value))
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 183
178 184
179 def _Delegate(resolve, reject, action): 185 def _Delegate(resolve, reject, action):
180 def _Run(x): 186 def _Run(x):
181 try: 187 try:
182 if action: 188 if action:
183 resolve(action(x)) 189 resolve(action(x))
184 else: 190 else:
185 resolve(x) 191 resolve(x)
186 except Exception as e: 192 except Exception as e:
193 # Adding traceback similarly to python 3.0 (pep-3134)
194 e.__traceback__ = sys.exc_info()[2]
187 reject(e) 195 reject(e)
188 return _Run 196 return _Run
OLDNEW
« no previous file with comments | « mojo/public/python/mojo/bindings/messaging.py ('k') | mojo/public/python/mojo/bindings/reflection.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698