MCPcopy
hub / github.com/jisaacks/GitGutter / Promise

Class Promise

modules/promise.py:16–163  ·  view source on GitHub ↗

A simple implementation of the Promise specification. See: https://promisesaplus.com Promise is in essence a syntactic sugar for callbacks. Simplifies passing values from functions that might do work in asynchronous manner. Example usage: * Passing return value of one funct

Source from the content-addressed store, hash-verified

14
15
16class Promise(object):
17 """A simple implementation of the Promise specification.
18
19 See: https://promisesaplus.com
20
21 Promise is in essence a syntactic sugar for callbacks. Simplifies passing
22 values from functions that might do work in asynchronous manner.
23
24 Example usage:
25
26 * Passing return value of one function to another:
27
28 def do_work_async(resolve):
29 # "resolve" is a function that, when called with a value, resolves
30 # the promise with provided value and passes the value to the next
31 # chained promise.
32 resolve(111) # Can be invoked asynchronously.
33
34 def process_value(value):
35 assert value === 111
36
37 Promise(do_work_async).then(process_value)
38
39 * Returning Promise from chained promise:
40
41 def do_work_async_1(resolve):
42 # Compute value asynchronously.
43 resolve(111)
44
45 def do_work_async_2(resolve):
46 # Compute value asynchronously.
47 resolve(222)
48
49 def do_more_work_async(value):
50 # Do more work with the value asynchronously. For the sake of this
51 # example, we don't use 'value' for anything.
52 assert value === 111
53 return Promise(do_work_async_2)
54
55 def process_value(value):
56 assert value === 222
57
58 Promise(do_work_async_1).then(do_more_work_async).then(process_value)
59 """
60
61 def __init__(self, executor):
62 """Initialize Promise object.
63
64 Arguments:
65 executor: A function that is executed immediately by this Promise.
66 It gets passed a "resolve" function. The "resolve" function, when
67 called, resolves the Promise with the value passed to it.
68 """
69 self.value = None
70 self.resolved = False
71 self.mutex = threading.Lock()
72 self.callbacks = []
73 self._invoke_executor(executor)

Callers 7

callbackMethod · 0.90
git_read_fileMethod · 0.85
execute_asyncMethod · 0.85
thenMethod · 0.85

Calls

no outgoing calls

Tested by 4

callbackMethod · 0.72