Search code examples
jquerypromisees6-promise

Window load not firing in promise callback


The following is avery basic example of what im trying to achieve. I have this working in chrome/FF but IE refuses to play nice.

somePromiseThatFetchesData.then((data)=>{

    $(document).ready(function(){
        var markup = functionThatGeneratesMarkup(data);
        $('body').html(markup);
    });

    $(window).on('load', function(){
        alert('all images etc from promise loaded finishes loading')
    });

});

The goal for me is to populate my dom with the fetched data, then wait until the assets in my generated data have loaded before firing another function. The problem is the alert in my callback never gets fired in IE

Im using the es6-promise polyfill for IE


Solution

  • You're not using a good strategy. If the load event has already fired before you attach the event handler, the handler will never execute.

    Instead, attach the events right away and then have your promise handlers inside them:

    $(document).ready(function(){
        somePromiseThatFetchesData.then((data)=>{
            var markup = functionThatGeneratesMarkup(data);
            $('body').html(markup);
        });
    });
    
    $(window).on('load', function(){
        somePromiseThatFetchesData.then((data)=>{
            alert('all images etc from promise loaded finishes loading')
        });
    });