p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

[p5.js 2.0 Docs] Change in return value for load functions that use callbacks needs to be documented

Open sidwellr opened this issue 1 year ago • 9 comments

Topic

In version 1.x, the load... functions (loadImage, loadJSON, etc.) always returned the loaded data, whether or not callbacks were used. This changed in version 2: now when load.. functions are used with callbacks, they now return the value returned by the callback instead of the loaded item. This is a nice feature that allows a successCallback to dynamically modify the loaded data. It also allows an errorCallback to return some default data. But this feature needs to be described in the documentation for the various load... functions. The need for this was noted by @limzykenneth in a comment on a previous issue, but it looks like it was missed.

This is a breaking change from version 1 to 2 since most version 1 sketches that use both an assignment and a callback function won't return the desired data from the callback function (this has actually broken one of the examples on the p5.js website; see /processing/p5.js-website#1033), so this should be emphasized in the documentation.

sidwellr avatar Nov 19 '25 23:11 sidwellr

Most load functions should already mention this feature, I think it is only loadFont() that does not mention this behavior, unless you found others?

limzykenneth avatar Nov 20 '25 10:11 limzykenneth

I noticed it with loadTable from one of the examples on the p5.js website. I checked a few others (the first few found when searching for "load") and none of them mentioned it either, so I created this issue.

The load functions that need this update are loadBlob, loadBytes, loadFilterShader, loadFont, and loadTable.

sidwellr avatar Nov 20 '25 16:11 sidwellr

I looked at the code for loadFilterShader, and it doesn't appear to return the value from the callback functions. It probably should for consistency with the other functions. Can fixing this be included with this issue? Or should a separate issue be created for it?

sidwellr avatar Nov 20 '25 17:11 sidwellr

Hi! This issue looks very clear and well-scoped. I’d be interested in working on documenting this behavior change for the listed load functions in p5.js 2.0 (documentation only). Please let me know if it’s okay for me to take this on.

aryashi100 avatar Dec 25 '25 22:12 aryashi100

I'm not sure if loadFilterShader or loadFont need this behaviour because there's not a meaningful way to modify a p5.Shader or p5.Font object before returning it the way you could with loaded JSON, or even an image (via updating its pixels.) I wonder if enforcing consistency in that case would lead to more confusion?

davepagurek avatar Dec 26 '25 14:12 davepagurek

That makes sense thanks for pointing that out. I’ll limit the documentation updates to the load functions where returning a modified value from the callback is meaningful, and avoid enforcing consistency for loadFont and loadFilterShader to prevent confusion.

aryashi100 avatar Dec 26 '25 20:12 aryashi100

But loadFont does return the value from the callback, whether that is meaningful or not, so that behavior should be documented, especially since it is different from version 1. For loadFilterShader, I personally find the inconsistency more confusing; I think its behavior should match loadShader. But that may just be me.

For both, it makes sense for the error callback to return a default font or shader if the requested one can't be loaded; currently, this is possible with loadFont (and loadShader) but not loadFilterShader.

sidwellr avatar Dec 26 '25 21:12 sidwellr

@davepagurek I think just in terms of overall consistency and future proofing we probably still want to have the functionality of resolving to callback return, this is a relatively niche usage anyway and usually I would expect when editing p5.Shader or p5.Font is not something someone would expect, they won't be using both callbacks and assignment.

Alternatively, we can make it so that if the success callback return undefined, it resolves to the loaded object; in the case of error I'm a bit less clear how it should work in this scenario though.

limzykenneth avatar Dec 26 '25 22:12 limzykenneth

Making it so that the functions return the loaded object if the success callback returns undefined would match the 1.x behavior (and make it easier to migrate sketches to 2.0). In the case of error, I suggest making that work like 1.x as well: return nothing (so no change needed).

sidwellr avatar Dec 26 '25 22:12 sidwellr