Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/content/examples/en/Parellel_Loading_Promise_all/code.js
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor/spelling: "untill" -> "until"

[img1, img2, img3] = await Promise.all([
loadImageAsync('https://picsum.photos/100/100?random=1'), // Replace the image links with user wanted images.
Copy link
Member

Choose a reason for hiding this comment

The 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 please? I tried the example and loadImage here works just as well, and the helper function does not actually have any other params - if you had another idea with this, please let me know, but if it's ok with you, I'd suggest the shorter example with only loadImage here.

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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading