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

trackFailures option

Open MarkusPint opened this issue 9 years ago • 2 comments

A basic option to track & inspect loading failures as described in https://github.com/addyosmani/basket.js/issues/152

  • Added detection of timeout error.
  • Added 3 tests.
  • This will not work with thenRequire() chaining.

Usage:

basket
    .require([{ url: 'missing.js' }], { trackFailures: true })
    .then(function (responses) {
        if ( responses[0].state === "rejected" ) {
            console.log(responses[0].reason.message); // Not Found
        }
    }, function(error) {
        // This is not called
    });

MarkusPint avatar Sep 22 '15 11:09 MarkusPint

cc @foxx

addyosmani avatar Nov 26 '15 17:11 addyosmani

I'm not keen on the idea of changing the promise chaining based on an argument, as it doesn't feel clean imho. Instead of trackFailures, perhaps we could use a third argument as seen in other thenables, which would be used to track progress, see here.

For example;

basket.require(
    { url: '//example.com', key: 'jquery' },
    { url: '//example.com', key: 'other' })
    .then(function(){
        console.log("All loaded okay");
    }, function(err) {
        console.log("Failed to load all scripts");
    }, function(event, args) {
        // event=loaderror
        // args={key:'jquery':, url: 'http://...', reason: ..., err: ...]
     })

Another option is introducing on and fail, like such;

basket.require(
    { url: '//example.com', key: 'jquery' },
    { url: '//example.com', key: 'other' })
    .then(function(){
        console.log("All loaded okay");
    })
    .fail(function(err) {
        console.log("Failed to load all scripts");
    })
    .on('loaderror', function(key, url, reason, err) {
        // {key:'jquery':, url: 'http://...', reason: ..., err: ...]
     })

These all get the same job done, just exposed in different ways. @addyosmani do you have any preference?

foxx avatar Nov 26 '15 21:11 foxx