Content-Length: 350100 | pFad | http://github.com/micropython/micropython/commit/be1ecb54e6be1ee5017a1a8aeca83d9e12ad16c9

DA webassembly/api: Resolve thenables returned from runPythonAsync. · micropython/micropython@be1ecb5 · GitHub
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

Diff for: ports/webassembly/api.js

+5-1
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"]);
+24
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.
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)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/be1ecb54e6be1ee5017a1a8aeca83d9e12ad16c9

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy