|
1137 | 1137 | * </div>
|
1138 | 1138 | */
|
1139 | 1139 |
|
| 1140 | + |
| 1141 | +/** |
| 1142 | + * Asynchronous Asset Loading with Async/Await. |
| 1143 | + * |
| 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 | + * |
| 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 | + * |
| 1160 | + * Nested callbacks require managing additional information and behavior. |
| 1161 | + * Lazy loading of assets with `async/await` can simplify control flow, |
| 1162 | + * but it also requires you to design your sketch around waiting for |
| 1163 | + * each operation to complete. |
| 1164 | + * |
| 1165 | + * Callbacks are still fully supported, so code that passes success / error |
| 1166 | + * functions to loaders like `loadImage()` or `loadJSON()` will behave exactly |
| 1167 | + * as it always has. This compatibility means sketches written with the older |
| 1168 | + * pattern don’t need any changes, and you can freely mix callbacks and |
| 1169 | + * `async/await` in the same project if that suits your workflow. |
| 1170 | + * |
| 1171 | + * In the example below, `setup()` is declared as an async function. We `await` |
| 1172 | + * the completion of both `loadImage()` and `loadJSON()` before calling |
| 1173 | + * `createCanvas()`. Only then does the sketch proceed, guaranteeing the assets |
| 1174 | + * are available for immediate use. |
| 1175 | + * |
| 1176 | + * ```js |
| 1177 | + * let img, data; |
| 1178 | + * |
| 1179 | + * async function setup() { |
| 1180 | + * // Wait until the image and JSON data have fully loaded. |
| 1181 | + * img = await loadImage("./my-image.png"); |
| 1182 | + * data = await loadJSON("./my-data.json"); |
| 1183 | + * |
| 1184 | + * // Once the assets are loaded, create the canvas. |
| 1185 | + * createCanvas(400, 400); |
| 1186 | + * } |
| 1187 | + * ``` |
| 1188 | + * |
| 1189 | + * @property async_await |
| 1190 | + * @example |
| 1191 | + * <div> |
| 1192 | + * <code> |
| 1193 | + * // Click and drag the mouse to view the scene from different angles. |
| 1194 | + * |
| 1195 | + * let shape; |
| 1196 | + * |
| 1197 | + * // Load the file and create a p5.Geometry object. |
| 1198 | + * async function setup() { |
| 1199 | + * shape = await loadModel('/assets/teapot.obj'); |
| 1200 | + * |
| 1201 | + * createCanvas(100, 100, WEBGL); |
| 1202 | + * |
| 1203 | + * describe('A white teapot drawn against a gray background.'); |
| 1204 | + * } |
| 1205 | + * |
| 1206 | + * function draw() { |
| 1207 | + * background(200); |
| 1208 | + * |
| 1209 | + * // Enable orbiting with the mouse. |
| 1210 | + * orbitControl(); |
| 1211 | + * |
| 1212 | + * // Draw the shape. |
| 1213 | + * model(shape); |
| 1214 | + * } |
| 1215 | + * </code> |
| 1216 | + * </div> |
| 1217 | + * |
| 1218 | + * <div> |
| 1219 | + * <code> |
| 1220 | + * let font; |
| 1221 | + * |
| 1222 | + * async function setup() { |
| 1223 | + * // Load a font for WebGL mode. |
| 1224 | + * font = await loadFont('assets/inconsolata.otf'); |
| 1225 | + * |
| 1226 | + * createCanvas(100, 100, WEBGL); |
| 1227 | + * |
| 1228 | + * describe( |
| 1229 | + * "A gray square. The mouse's x- and y-coordinates are displayed as the user moves the mouse." |
| 1230 | + * ); |
| 1231 | + * } |
| 1232 | + * |
| 1233 | + * function draw() { |
| 1234 | + * background(200); |
| 1235 | + * |
| 1236 | + * // Style the text. |
| 1237 | + * textAlign(CENTER); |
| 1238 | + * textSize(16); |
| 1239 | + * textFont(font); |
| 1240 | + * fill(0); |
| 1241 | + * |
| 1242 | + * // Display the mouse's coordinates. |
| 1243 | + * text(`x: ${mouseX} y: ${mouseY}`, 0, 0); |
| 1244 | + * } |
| 1245 | + * </code> |
| 1246 | + * </div> |
| 1247 | + */ |
| 1248 | + |
1140 | 1249 | /**
|
1141 | 1250 | * A list that keeps several pieces of data in order.
|
1142 | 1251 | *
|
|
0 commit comments