Create a new promise and chain it with this promise. When this promise gets resolved, the callback will be called with the value that this promise resolved with. Returns a new promise that can be used to do further chaining. Arguments: callback: The cal
(self, callback)
| 88 | return cls(executor) |
| 89 | |
| 90 | def then(self, callback): |
| 91 | """Create a new promise and chain it with this promise. |
| 92 | |
| 93 | When this promise gets resolved, the callback will be called with the |
| 94 | value that this promise resolved with. |
| 95 | |
| 96 | Returns a new promise that can be used to do further chaining. |
| 97 | |
| 98 | Arguments: |
| 99 | callback: The callback to call when this promise gets resolved. |
| 100 | """ |
| 101 | def callback_wrapper(resolve_fn, resolve_value): |
| 102 | """A wrapper called when this promise resolves. |
| 103 | |
| 104 | Arguments: |
| 105 | resolve_fn: A resolve function of newly created promise. |
| 106 | resolve_value: The value with which this promise resolved. |
| 107 | """ |
| 108 | result = callback(resolve_value) |
| 109 | # If returned value is a promise then this promise needs to be |
| 110 | # resolved with the value of returned promise. |
| 111 | if isinstance(result, Promise): |
| 112 | result.then(resolve_fn) |
| 113 | else: |
| 114 | resolve_fn(result) |
| 115 | |
| 116 | def sync_executor(resolve_fn): |
| 117 | """Call resolve_fn immediately with the resolved value. |
| 118 | |
| 119 | An executor that will immediately resolve resolve_fn with the |
| 120 | resolved value of this promise. |
| 121 | """ |
| 122 | callback_wrapper(resolve_fn, self._get_value()) |
| 123 | |
| 124 | def async_executor(resolve_fn): |
| 125 | """Queue resolve_fn to be called after this promise resolves later. |
| 126 | |
| 127 | An executor that will resolve received resolve_fn when this promise |
| 128 | resolves later. |
| 129 | """ |
| 130 | self._add_callback(functools.partial(callback_wrapper, resolve_fn)) |
| 131 | |
| 132 | if self._is_resolved(): |
| 133 | return Promise(sync_executor) |
| 134 | return Promise(async_executor) |
| 135 | |
| 136 | def _invoke_executor(self, executor): |
| 137 | def resolve_fn(new_value): |