Skip to content

Commit 3973982

Browse files
authored
Merge pull request #7721 from perminder-17/patch-3
Updating foundation-section for async/await
2 parents 7dddf55 + f0648d1 commit 3973982

File tree

4 files changed

+123
-13
lines changed

4 files changed

+123
-13
lines changed

lib/addons/p5.sound.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,11 +2945,10 @@ function () {
29452945
}();
29462946
/**
29472947
* loadSound() returns a new p5.SoundFile from a specified
2948-
* path. If called during preload(), the p5.SoundFile will be ready
2949-
* to play in time for setup() and draw(). If called outside of
2950-
* preload, the p5.SoundFile will not be ready immediately, so
2951-
* loadSound accepts a callback as the second parameter. Using a
2952-
* <a href="https://github.com/processing/p5.js/wiki/Local-server">
2948+
* path. If used with asynchronous setup (via `async`/`await`), the p5.SoundFile
2949+
* will be ready to play in time for `setup()` and `draw()`. If called outside of an async context,
2950+
* the p5.SoundFile will not be ready immediately, so loadSound accepts a callback as the second
2951+
* parameter. Using a <a href="https://github.com/processing/p5.js/wiki/Local-server">
29532952
* local server</a> is recommended when loading external files.
29542953
*
29552954
* @method loadSound

src/core/main.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import * as constants from './constants';
1414
* a p5 sketch. It expects an incoming sketch closure and it can also
1515
* take an optional node parameter for attaching the generated p5 canvas
1616
* to a node. The sketch closure takes the newly created p5 instance as
17-
* its sole argument and may optionally set <a href="#/p5/preload">preload()</a>,
18-
* <a href="#/p5/setup">setup()</a>, and/or
19-
* <a href="#/p5/draw">draw()</a> properties on it for running a sketch.
17+
* its sole argument and may optionally set an asynchronous function
18+
* using `async/await`, along with the standard <a href="#/p5/setup">setup()</a>,
19+
* and/or <a href="#/p5/setup">setup()</a>, and/or <a href="#/p5/draw">draw()</a>
20+
* properties on it for running a sketch.
2021
*
2122
* A p5 sketch can run in "global" or "instance" mode:
2223
* "global" - all properties and methods are attached to the window
@@ -466,6 +467,8 @@ for (const k in constants) {
466467
* <a href="#/p5/draw">draw()</a> begins looping. If the
467468
* <a href="#/p5/preload">preload()</a> is declared, then `setup()` will
468469
* run immediately after <a href="#/p5/preload">preload()</a> finishes
470+
*
471+
*
469472
* loading assets.
470473
*
471474
* Note: `setup()` doesn’t have to be declared, but it’s common practice to do so.

src/core/reference.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,115 @@
11371137
* </div>
11381138
*/
11391139

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+
11401249
/**
11411250
* A list that keeps several pieces of data in order.
11421251
*

src/utilities/time_date.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ function timeDate(p5, fn){
117117
*
118118
* If a sketch has a
119119
* <a href="#/p5/setup">setup()</a> function, then `millis()` begins tracking
120-
* time before the code in <a href="#/p5/setup">setup()</a> runs. If a
121-
* sketch includes a <a href="#/p5/preload">preload()</a> function, then
122-
* `millis()` begins tracking time as soon as the code in
123-
* <a href="#/p5/preload">preload()</a> starts running.
124-
*
120+
* time before the code in <a href="#/p5/setup">setup()</a> runs. If a
121+
* sketch includes asynchronous loading using `async`/`await`, then
122+
* `millis()` begins tracking time as soon as the asynchronous code
123+
* starts running.
125124
* @method millis
126125
* @return {Number} number of milliseconds since starting the sketch.
127126
*

0 commit comments

Comments
 (0)