require.ensure error handling #758

Closed
richardscarrott opened this Issue Feb 3, 2015 · 4 comments

Projects

None yet

4 participants

@richardscarrott

I'm implementing split points using commonJS however I can't see a way to handle failed chunk requests. I also haven't seen anything in the commonJS async proposal mentioning error handling. I have however seen implementations doing this:

require.ensure(['foo'], function(err) {
    if (err) {
        // error
        return;
    }
    // success
    var foo = require('foo');
});

Although I'm not sure if the spec requires the first argument to be require?

I think the same goes for AMD as I can't see anything in the spec re: error handling but almost all implementations do this:

require(['foo'], function(foo) {
    // success
}, function() {
    // error
});

In terms of implementation for webpack I imagine it could easily be handled by binding to the script tags 'error' event?

@zxcabs
zxcabs commented Feb 4, 2015

we are discussing it in #686 and #692

@sokra sokra added a commit that closed this issue Nov 6, 2015
@sokra sokra Chunk loading error handling
fixes #692
fixes #758
fixes #686
fixes #785
bf1f114
@sokra sokra closed this in bf1f114 Nov 6, 2015
@adriaanmeuris

So it seems this issue got resolved, but can't seem to find an example how to implement it and make sure to catch errors.
http://stackoverflow.com/questions/37578699/webpack-require-ensure-catch-jsonp-error

Any guidance how to do this, or do you still need a loader like https://github.com/NekR/async-module-loader for this?

@sokra
Member
sokra commented Jun 7, 2016

The webpack 2 API returns a Promise:

System.import("module").then(() => { ... }).catch(err => { ... });
@adriaanmeuris

Got it, didn't realise this was for v2 - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment