forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_python_async2.mjs
More file actions
42 lines (33 loc) · 1.06 KB
/
Copy pathrun_python_async2.mjs
File metadata and controls
42 lines (33 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Test runPythonAsync() and top-level await in Python, with multi-level awaits.
const mp = await (await import(process.argv[2])).loadMicroPython();
// simulate a 2-step resolution of the string "OK".
await mp.runPythonAsync(`
import js
def _timeout(resolve, _):
js.setTimeout(resolve, 100)
def _fetch():
return js.Promise.new(_timeout)
async def _text(promise):
if not promise._response:
print("_text await start")
await promise
print("_text awaited end")
ret = await promise._response.text()
return ret
class _Response:
async def text(self):
print('_Response.text start')
await js.Promise.new(_timeout)
print('_Response.text end')
return "OK"
def _response(promise):
promise._response = _Response()
return promise._response
def fetch(url):
promise = _fetch().then(lambda *_: _response(promise))
promise._response = None
promise.text = lambda: _text(promise)
return promise
print(await fetch("config.json").text())
print(await (await fetch("config.json")).text())
`);