|
1141 | 1141 | /**
|
1142 | 1142 | * Asynchronous Asset Loading with Async/Await.
|
1143 | 1143 | *
|
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. |
1149 | 1149 | *
|
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. |
1153 | 1165 | *
|
1154 | 1166 | * ```js
|
1155 | 1167 | * let img, data;
|
1156 | 1168 | *
|
1157 |
| - * async function setup(){ |
| 1169 | + * async function setup() { |
1158 | 1170 | * // Wait until the image and JSON data have fully loaded.
|
1159 | 1171 | * img = await loadImage("./my-image.png");
|
1160 | 1172 | * data = await loadJSON("./my-data.json");
|
1161 | 1173 | *
|
1162 |
| - * // Create the canvas after loading assets. |
| 1174 | + * // Once the assets are loaded, create the canvas. |
1163 | 1175 | * createCanvas(400, 400);
|
1164 | 1176 | * }
|
1165 | 1177 | * ```
|
1166 | 1178 | *
|
1167 |
| - * @property async/await |
| 1179 | + * @property async_await |
1168 | 1180 | * @example
|
1169 | 1181 | * <div>
|
1170 | 1182 | * <code>
|
1171 | 1183 | * let img, data;
|
1172 | 1184 | *
|
1173 |
| - * async function setup(){ |
| 1185 | + * async function setup() { |
1174 | 1186 | * // Pause execution until the image is loaded.
|
1175 | 1187 | * img = await loadImage("./assets/image.png");
|
1176 | 1188 | *
|
|
1184 | 1196 | * background(220);
|
1185 | 1197 | * image(img, 0, 0);
|
1186 | 1198 | *
|
1187 |
| - * // Log the loaded JSON data to the console. |
| 1199 | + * // Log the loaded JSON data to the console for debugging or inspection. |
1188 | 1200 | * console.log(data);
|
1189 | 1201 | * }
|
1190 | 1202 | * </code>
|
|
0 commit comments