Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Loading javascript with a Promise fails on hard refresh

In my code I'm scanning a page to see if we find anything with a class of "coceon" (an image comparison JS lib), if found we then load in the cocoen scripts to then run it.

The code:

/* cocoen image comparison */
if ($('.cocoen').length)
{
    var cocoen_js = Promise.resolve($.cachedScript("/includes/jscripts/cocoen/js/cocoen.min.js"));
    var cocoean_jquery = Promise.resolve($.cachedScript("/includes/jscripts/cocoen/js/cocoen-jquery.min.js"));

    Promise.all([cocoen_js, cocoean_jquery]).then(() => { 
        $('.cocoen').cocoen();
      });
}

The trouble is, sometimes it just fails with the browser console telling me this line:

$('.cocoen').cocoen();

Fails as cocoen is not a function. So sometimes it's not loaded ready when needed. Especially so when doing a hard refresh, something seems to block it? Is there a way to ensure it's actually loaded to then run?

The cache script code if you need to see it too:

jQuery.cachedScript = function( url, options ) 
{
    // Allow user to set any option except for dataType, cache, and url
    options = $.extend( options || {}, {
      dataType: "script",
      cache: true,
      url: url
    });
   
    // Use $.ajax() since it is more flexible than $.getScript
    // Return the jqXHR object so we can chain callbacks
    return jQuery.ajax( options );
};
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!