-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
question: how to load a json file in preload only if it exists #3416
Comments
Does it block execution? Does it show with p5.min.js? Is the error callback called when file is not present? The first message looks like something FES throws. The second message is it coming from this line? |
With both minified and non-minified v0.7.2 the sketch will hang in preload if the json file is not found, even if surrounded by a try/catch (as you suggest, the error above only shows with FES enabled). When a callback function is provided, things do work correctly. So not a big problem, as there is a workaround, but perhaps should be documented at least. |
i'm not sure if this is intentional or not, but i think it's because the Lines 155 to 164 in 1f14632
if we want failure to also progress to setup() , then i think the 'error' callback also needs to call _decrementPreload .
on a completely separate note. i noticed that the error behavior is different for these functions calling Line 578 in 1f14632
... and others Line 172 in 1f14632
|
I think this is intentional behaviour because when you are loading a file in preload without error callbacks the rest of the sketch code may be expecting sucessful load of the file if they are executed. If the sketch proceeds from preload even if it is a 404 the rest of the sketch may just error out itself expecting a file to have already been loaded. @Spongman The inconsistency probably is due to some legacy code mixed with new code. It probably should be thrown to halt execution. |
Is there a reason not to throw an error for the preload case, which the user could catch in the admittedly rare case where this was useful? Sketch would still terminate normally if not caught.. |
the problem is that the |
I don't think we can reasonably achieve this in the current setup. let a;
function preload() {
a = loadJSON(url, () => {}, () => this._decrementPreload());
} But obviously @Spongman you are right that throwing in those callbacks is a bit strange - mostly they just cause an uncaught promise rejection, which in most browsers is just ignored. I converted some of these to This issue has implications for the promised-based revamp of this loading system (#2698) and so I'm interested in how we would want this to work going forward. In particular, we've yet to find a suitably beginner-friendly way to support preloading blocking execution that isn't confusing. Alternatively, if we had support for let a;
async function setup() {
try {
a = await loadJSONAsync(url); // This is not implemented yet
} catch (e) {}
} Or with current fns: let a;
async function setup() {
try {
a = await new Promise((resolve, reject) => loadJSON(url, resolve, reject));
} catch (e) {}
} |
i'm strongly in favor of the
|
I think eventually we will want to move to better support promise based |
closing this because it is as @limzykenneth points out intended behavior and I believe difficult to implement with the current system. |
Tried this with with a try/catch in preload and got the error below, after which the sketch hangs on 'Loading'. Might be nice if there was a way to load, for example, a .json config file in preload (without being forced to use the callback version of loadJSON) and have the sketch continue if its not there...
The text was updated successfully, but these errors were encountered: