Skip to content

Commit af49775

Browse files
committed
Document the new async behaviour.
1 parent cf41d55 commit af49775

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

docs/api.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ Pyodide and MicroPython. It is closely modelled on the
197197
[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) found
198198
in browsers with some important Pythonic differences.
199199

200-
The simple use case is to pass in a URL and `await` the response. Remember, in
201-
order to use `await` you must have the `async` attribute in the `script` tag
202-
that references your code. If this request is in a function, that function
203-
should also be defined as `async`.
200+
The simple use case is to pass in a URL and `await` the response. If this
201+
request is in a function, that function should also be defined as `async`.
204202

205203
```python title="A simple HTTP GET with pyscript.fetch"
206204
from pyscript import fetch
@@ -834,7 +832,7 @@ While over on the main thread, this fragment of MicroPython will be able to
834832
access the worker's `version` function via the `workers` reference:
835833

836834
```html
837-
<script type="mpy" async>
835+
<script type="mpy">
838836
from pyscript import workers
839837
840838
pyworker = await workers["py-version"]
@@ -853,7 +851,7 @@ Should you wish to await for all workers on the page at load time, it's
853851
possible to loop over matching elements in the document like this:
854852

855853
```html
856-
<script type="mpy" async>
854+
<script type="mpy">
857855
from pyscript import document, workers
858856
859857
for el in document.querySelectorAll("[type='py'][worker][name]"):
@@ -872,7 +870,7 @@ an asynchronous way to import packages that were not originally referenced in
872870
your configuration.
873871

874872
```html title="A pyscript.js_import example."
875-
<script type="py" async>
873+
<script type="py">
876874
from pyscript import js_import, window
877875
878876
escaper, = await js_import("https://esm.run/html-escaper")
@@ -899,7 +897,7 @@ asynchronous way to import packages that were not originally referenced in your
899897
configuration.
900898
901899
```html title="A pyscript.py_import example."
902-
<script type="py" async>
900+
<script type="py">
903901
from pyscript import py_import
904902
905903
matplotlib, regex, = await py_import("matplotlib", "regex")

docs/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ Combined with our `pyscript.fetch` utility, it's also possible to store more
819819
complex data from the web.
820820

821821
```python title="Writing a binary file."
822-
# Assume an `async` attribute / execution.
822+
# Assume async execution.
823823
from pyscript import fetch, window
824824

825825
href = window.location.href

docs/user-guide/first-steps.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,27 @@ attributes:
7575
JSON or a TOML file,
7676
`config='{"packages":["numpy"]}'` and `config="./config.json"` or
7777
`config="./config.toml"` are all valid.
78-
* `async` - your Python code can contain a
79-
[top level await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await).
78+
* `async` - set this flag to `"false"` so your code won't not run within a
79+
[top level await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await)
80+
(the default behaviour).
81+
82+
83+
!!! warning
84+
85+
**This behaviour changed in version 2024.8.2.**
86+
87+
PyScript now uses a
88+
[top level await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await)
89+
by default.
90+
91+
You used to have to include the `async` flag to enable this. Now, instead,
92+
use `async="false"` to revert the behaviour back to the old default
93+
behaviour of synchronous evaluation.
94+
95+
We made this change because many folks were `await`-ing functions and
96+
missing or not realising the need for the (old) `async` attribute. Hence
97+
the flip in behaviour.
98+
8099
* `worker` - a flag to indicate your Python code is to be run on a
81100
[web worker](workers.md) instead of the "main thread" that looks after the user
82101
interface.

docs/user-guide/plugins.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ on a web worker, the exact same sequence of steps takes place:
4242
callback found in test frameworks.
4343

4444
PyScript's interpreters can run their code either *synchronously* or
45-
*asynchronously*. No matter, the very same sequence is guaranteed to run in
46-
order in both cases, the only difference being the naming convention used to
47-
reference synchronous or asynchronous lifecycle hooks.
45+
*asynchronously* (**note**, the default is asynchronously). No matter, the very
46+
same sequence is guaranteed to run in order in both cases, the only difference
47+
being the naming convention used to reference synchronous or asynchronous
48+
lifecycle hooks.
4849

4950
### Hooks
5051

docs/user-guide/workers.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,7 @@ Here's how:
286286
<!-- This script tag bootstraps PyScript -->
287287
<script type="module" src="https://pyscript.net/releases/2024.8.2/core.js"></script>
288288
<title>PyWorker - mpy bootstrapping pyodide example</title>
289-
<!-- the async attribute is useful but not mandatory -->
290-
<script type="mpy" src="main.py" async></script>
289+
<script type="mpy" src="main.py"></script>
291290
</head>
292291
</html>
293292
```

0 commit comments

Comments
 (0)