Skip to content

Commit cf585ff

Browse files
authored
polish docs
1 parent 762c1fb commit cf585ff

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/core/reference.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,36 +1141,48 @@
11411141
/**
11421142
* Asynchronous Asset Loading with Async/Await.
11431143
*
1144-
* Using async/await in your p5.js sketch allows you to load assets, such as images and JSON data,
1145-
* in a straightforward, linear fashion without resorting to nested callbacks. Declaring a function
1146-
* with the async keyword tells JavaScript that this function will perform asynchronous operations and
1147-
* will always return a promise. Within an async function, the await keyword pauses the execution until
1148-
* the promise is resolved, making the code appear synchronous and easier to read and maintain.
1144+
* The keywords `async` and `await` let you write asynchronous code in a more
1145+
* straightforward, linear style. Instead of nesting callbacks or juggling
1146+
* multiple promise chains, you can pause execution at `await` while waiting
1147+
* for a promise to resolve. This makes your code flow more naturally, as if
1148+
* it were synchronous.
11491149
*
1150-
* In the example below, setup() is declared as an async function. The await keyword is used to wait for
1151-
* the loadImage() and loadJSON() functions to complete their asset loading. Once the assets are loaded,
1152-
* createCanvas() sets up the drawing surface and the assets become available for use.
1150+
* When you mark a function with the `async` keyword—like `async function setup() {...}`—it
1151+
* signals that the function contains asynchronous operations and will return a
1152+
* promise. Any time you use the `await` keyword inside this function, JavaScript
1153+
* will pause the function’s execution until the awaited promise settles.
1154+
*
1155+
* In p5.js, you can use `async/await` to handle media loading functions such as
1156+
* `loadImage()`, `loadJSON()`, `loadSound()`, and so on. This allows you to:
1157+
* - load files in a more readable, top-to-bottom manner
1158+
* - decide when the assets are fully available before proceeding
1159+
* - avoid nested callbacks often referred to as "callback hell"
1160+
*
1161+
* In the example below, `setup()` is declared as an async function. We `await`
1162+
* the completion of both `loadImage()` and `loadJSON()` before calling
1163+
* `createCanvas()`. Only then does the sketch proceed, guaranteeing the assets
1164+
* are available for immediate use.
11531165
*
11541166
* ```js
11551167
* let img, data;
11561168
*
1157-
* async function setup(){
1169+
* async function setup() {
11581170
* // Wait until the image and JSON data have fully loaded.
11591171
* img = await loadImage("./my-image.png");
11601172
* data = await loadJSON("./my-data.json");
11611173
*
1162-
* // Create the canvas after loading assets.
1174+
* // Once the assets are loaded, create the canvas.
11631175
* createCanvas(400, 400);
11641176
* }
11651177
* ```
11661178
*
1167-
* @property async/await
1179+
* @property async_await
11681180
* @example
11691181
* <div>
11701182
* <code>
11711183
* let img, data;
11721184
*
1173-
* async function setup(){
1185+
* async function setup() {
11741186
* // Pause execution until the image is loaded.
11751187
* img = await loadImage("./assets/image.png");
11761188
*
@@ -1184,7 +1196,7 @@
11841196
* background(220);
11851197
* image(img, 0, 0);
11861198
*
1187-
* // Log the loaded JSON data to the console.
1199+
* // Log the loaded JSON data to the console for debugging or inspection.
11881200
* console.log(data);
11891201
* }
11901202
* </code>

0 commit comments

Comments
 (0)