-
Notifications
You must be signed in to change notification settings - Fork 146
Adding documentation Example on how to do parallel loading with Promi… #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// PromiseAllExample.js | ||
|
||
// Declare variables to hold the images we'll load | ||
let img1, img2, img3; | ||
|
||
async function setup() { | ||
// Add screen reader-friendly text description | ||
textOutput(); | ||
|
||
// Create a canvas where the images will be drawn | ||
createCanvas(600, 400); | ||
|
||
// Set background color to gray | ||
background(220); | ||
|
||
// Configure text appearance | ||
textAlign(CENTER, CENTER); | ||
textSize(18); | ||
|
||
// Use async/await with Promise.all to load all three images at once | ||
// This waits untill ALL images are loaded before continuing | ||
[img1, img2, img3] = await Promise.all([ | ||
loadImageAsync('https://picsum.photos/100/100?random=1'), // Replace the image links with user wanted images. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the detailed documentation here! Could you explain the logic a bit more behind the helper |
||
loadImageAsync('https://picsum.photos/100/100?random=2'), | ||
loadImageAsync('https://picsum.photos/100/100?random=3') | ||
]); | ||
|
||
// Once all images are ready, draw them on the canvas | ||
image(img1, 100, 150); // Draw first image at x=100 | ||
image(img2, 250, 150); // Second image at x=250 | ||
image(img3, 400, 150); // Third image at x=400 | ||
|
||
// Display a message showing that everything is loaded | ||
fill(0); // Set text color to black | ||
text("All images loaded!", width / 2, 50); | ||
} | ||
|
||
// Helper function to load images using a Promise | ||
// Makes loadImage compatible with async/await style | ||
function loadImageAsync(url) { | ||
return new Promise((resolve, reject) => { | ||
// Try to load the image from the given URL. | ||
//If successful, resolve the promise with the image. If it fails, reject with the error. | ||
loadImage(url, img => resolve(img), err => reject(err)); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
featuredImage: "../../../images/featured/16_Async_Await_PromiseAll-thumbnail.png" | ||
featuredImageAlt: Three random images loaded and displayed on a canvas after using async/await and Promise.all. | ||
title: Async Await with Promise.all | ||
oneLineDescription: Load multiple resources asynchronously before drawing. | ||
--- | ||
|
||
This example demonstrates how to use | ||
<a href="https://beta.p5js.org/reference/p5/async_await/" target="_blank"> async/await </a> | ||
together with | ||
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all" target="_blank">Promise.all()</a> | ||
|
||
Three random images are fetched asynchronously from the internet. | ||
After all images finish loading, they are drawn together on the canvas. | ||
Using | ||
<a href="https://beta.p5js.org/reference/p5/loadimage/" target="_blank">loadImage()</a> | ||
wrapped inside a promise allows better control over loading multiple resources efficiently. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor/spelling: "untill" -> "until"