This message was deleted.
# help
s
This message was deleted.
m
Sure.
require
"simply" returns a promise, so we can use JS's promise tools to work with it. Assuming
unpkg
is returning an error, you can catch it and swap in the other provider
Copy code
P5 = require('<https://unpkg.com/p5@1.2.0/lib/p5.js').catch(()> => require("<https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js>"))
💖 2
You could probably also set up a
Promise.race()
situation where you try both in parallel, that seems heavy handed though
a
You are faster and more elegant. I arrived at the same only after lots of code and using two cells (one to define the function and one to call it):
Copy code
evalP5 = () => {
  let message = '';
  try {
    require('<https://unpkg.com/p5@1.2.0/lib/p5.js>');
    message = 'p5.js loaded successfully.';
    return require('<https://unpkg.com/p5@1.2.0/lib/p5.js>')
  } catch (error) {
    console.log('Failed to load p5.js from <http://unpkg.com|unpkg.com>. Trying cdnjs.com...');
    try {
      require('<https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js>');
      message = 'p5.js loaded successfully.';
      return require('<https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js>')
    } catch (error) {
      console.error('Failed to load p5.js from <http://cdnjs.com|cdnjs.com>:', error);
      message = "nothing's working";
      return message;
    }
  }
}
🫶 1
m
Oh, heads up. I notice that in both my example and Brett's original, the versions of p5.js are different (1.2 in the unpkg branch vs 1.6 in the fallback). It would be best to use the same fixed version
👍 1
Also, I hear that jsdelivr often works better than any of the above: https://www.jsdelivr.com/.
👍 1
f
You can just drop the host to have it fall back to cdn.observableusercontent.com:
Copy code
require('p5@1.2.0/lib/p5.js')
👍 1
b
So I could just have catches all the way down.
P5 = require('<https://unpkg.com/p5@1.6.0/lib/p5.js').catch(()> => require("<https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js>")).catch(() => require("<https://cdn.jsdelivr.net/npm/p5@1.6.0/lib/p5.min.js>"))
🐢 1
f
please don't 🙂 just use jsdelivr or leave the host off
b
if the hosts off will it try different hosts until if gets a win?
f
until now i didn't even realize that "digital prepping" can exist 😬
seriously though, you're preparing for a case that will never happen and convoluting your code in the process. just don't use unpkg.com which is by far one of the most unreliable services.
if the hosts off will it try different hosts until if gets a win?
@Brett Cooper sorry, bit late here, i misread your question: no, it will default to cdn.observableusercontent.com which uses jsdelivr under the hood
but i can't remember a single instance where jsdelivr went away
b
ok thanks
🙏 1
a
apparently several times in March? - no, those are just 'warn'
https://status.jsdelivr.com/ - no incidents 🙂
100% global uptime in the last year!
🎉 1
f
@aaronkyle those outages in the first link are reported for data.jsdelivr.com
a
i realized the first link wasn't great and found the actual status report after. i was curious.
f
yeah, tbh i wouldn't trust that site (the first one)
☝🏻 1
b
I just commented on https://observablehq.com/@tmcw/p5 with a request to update the link as this is the main way people access p5 on observable.