Closed nickmcintyre closed 1 week ago
@nickmcintyre Sorry I'm not super sure how this differs from #6767 in that they both are about implementing promise/async/await based loading?
@limzykenneth oops, definitely worth clarifying. I thought it might be helpful to lightly decouple the discussion about async setup()
and preload()
from a discussion about keeping callbacks or fully adopting Promises (i.e., .catch()
and .error()
). They're closely related, but the latter hasn't really been addressed in #6767. So, I created a space for it.
most of the JavaScript ecosystem has migrated to Promises
This is mostly true but callbacks are still commonly used in JS event and array functions.
I guess to clarify, callbacks for non-asynchronous cases (e.g. arrays) and ones without a single event to listen to (e.g. DOM event listeners) are still the standard. For all cases where promises are appropriate (asynchronous and involve waiting on a single future event) it seems that core js APIs have moved to promises.
Increasing access
Asynchronous JavaScript can be confusing, especially for beginners. A simple, consistent, and standard programming model could help to smooth out everyone's learning curve.
Which types of changes would be made?
Most appropriate sub-area of p5.js?
What's the problem?
p5.js' asynchronous programming model currently uses callbacks. It works, but most of the JavaScript ecosystem has migrated to Promises because they're easier to reason about, especially when used with async/await.
What's the solution?
I suggest that we use Promises and async/await consistently across the API. Most asynchronous functions uses Promises internally, so the implementation would probably be straightforward.
Here's an example of loading an image based on feedback in #6767:
And here's a possible revamp for
httpGet()
based on feedback in #7090:Note: This already works.
Parallelism
There's an open question about the best way to handle parallelism.
preload()
currently manages this with magic, which is nice. A proposed solution for async/await would use Promse.all() behind the scenes and return an array:Or:
Or, treading lightly here:
This optimization probably isn't needed for iterative work, but it's definitely helpful for sharing sketches. My sense is that beginners are usually ready for arrays by the time they need to draw a litter of kittens.
Pros (updated based on community comments)
Cons (updated based on community comments)
TBD
Proposal status
Under review