Files
micropython/tests/ports/webassembly/asyncio_top_level_await.mjs
Damien George e9c898cb33 webassembly/asyncio: Support top-level await of asyncio Task and Event.
This change allows doing a top-level await on an asyncio primitive like
Task and Event.

This feature enables a better interaction and synchronisation between
JavaScript and Python, because `api.runPythonAsync` can now be used (called
from JavaScript) to await on the completion of asyncio primitives.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-18 22:23:16 +10:00

26 lines
516 B
JavaScript

// Test top-level await on asyncio primitives: Task, Event.
const mp = await (await import(process.argv[2])).loadMicroPython();
await mp.runPythonAsync(`
import asyncio
async def task(event):
print("task set event")
event.set()
print("task sleep")
await asyncio.sleep(0.1)
print("task end")
event = asyncio.Event()
t = asyncio.create_task(task(event))
print("top-level wait event")
await event.wait()
print("top-level wait task")
await t
print("top-level end")
`);
console.log("finished");