require.ensure error handling

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?

Author: Fantashit

1 thought on “require.ensure error handling

  1. The webpack 2 API returns a Promise:

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

Comments are closed.