MCPcopy Index your code
hub / github.com/pyscript/pyscript / _FetchPromise

Class _FetchPromise

core/src/stdlib/pyscript/fetch.py:91–147  ·  view source on GitHub ↗

Wraps the fetch promise to enable direct method chaining. This allows calling response methods directly on the fetch promise: `await fetch(url).json()` instead of requiring two separate awaits. This feels more Pythonic since it matches typical usage patterns Python developers

Source from the content-addressed store, hash-verified

89
90
91class _FetchPromise:
92 """
93 Wraps the fetch promise to enable direct method chaining.
94
95 This allows calling response methods directly on the fetch promise:
96 `await fetch(url).json()` instead of requiring two separate awaits.
97
98 This feels more Pythonic since it matches typical usage patterns
99 Python developers have got used to via libraries like `requests`.
100 """
101
102 def __init__(self, promise):
103 self._promise = promise
104 # To be resolved in the future via the setup() static method.
105 promise._response = None
106 # Add convenience methods directly to the promise.
107 promise.arrayBuffer = self.arrayBuffer
108 promise.blob = self.blob
109 promise.bytearray = self.bytearray
110 promise.json = self.json
111 promise.text = self.text
112
113 @staticmethod
114 def setup(promise, response):
115 """
116 Store the resolved response on the promise for later access.
117 """
118 promise._response = _FetchResponse(response)
119 return promise._response
120
121 async def _get_response(self):
122 """
123 Get the cached response, or await the promise if not yet resolved.
124 """
125 if not self._promise._response:
126 await self._promise
127 return self._promise._response
128
129 async def arrayBuffer(self):
130 response = await self._get_response()
131 return await response.arrayBuffer()
132
133 async def blob(self):
134 response = await self._get_response()
135 return await response.blob()
136
137 async def bytearray(self):
138 response = await self._get_response()
139 return await response.bytearray()
140
141 async def json(self):
142 response = await self._get_response()
143 return await response.json()
144
145 async def text(self):
146 response = await self._get_response()
147 return await response.text()
148

Callers 1

fetchFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected