Skip to content

Commit be1ecb5

Browse files
committed
webassembly/api: Resolve thenables returned from runPythonAsync.
JavaScript semantics are such that the caller of an async function does not need to await that function for it to run to completion. This commit makes that behaviour also apply to top-level async Python code run via `runPythonAsync()`. Signed-off-by: Damien George <damien@micropython.org>
1 parent c056840 commit be1ecb5

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

ports/webassembly/api.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ export async function loadMicroPython(options) {
152152
[buf, len, value],
153153
);
154154
Module._free(buf);
155-
return proxy_convert_mp_to_js_obj_jsside_with_free(value);
155+
const ret = proxy_convert_mp_to_js_obj_jsside_with_free(value);
156+
if (ret instanceof PyProxyThenable) {
157+
return Promise.resolve(ret);
158+
}
159+
return ret;
156160
},
157161
replInit() {
158162
Module.ccall("mp_js_repl_init", "null", ["null"]);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Test runPythonAsync() without await'ing it.
2+
3+
const mp = await (await import(process.argv[2])).loadMicroPython();
4+
5+
globalThis.p = new Promise((resolve, reject) => {
6+
setTimeout(() => {
7+
resolve(123);
8+
console.log("setTimeout resolved");
9+
}, 100);
10+
});
11+
12+
console.log(1);
13+
14+
const ret = mp.runPythonAsync(`
15+
import js
16+
print("py 1")
17+
print("resolved value:", await js.p)
18+
print("py 2")
19+
`);
20+
21+
// `ret` should be a Promise.
22+
console.log(2, ret);
23+
24+
// Here, the Python async code should continue to run until completed.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1
2+
2 Promise { <pending> }
3+
py 1
4+
setTimeout resolved
5+
resolved value: 123
6+
py 2

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy