So I'm writing an app using RequireJS and Socket.io that checks to see if the socket.io resource is available and then, on connection, bootstraps the app. In case socket.io ever temporarily goes down I'd like to have requireJS poll for the resource a few times until it is available and then continue on initializing the application.
Unfortunately (or maybe fortunately?) it seems like there is some sort of caching mechanism in require that registers scripterrors for scripts that don't load so that if you do a setTimeout in the error callback that retrys the socketio require function, require will continue to throw errors even when the resource becomes available.
Is this an oversight or is there a reason for keeping this error cached? More importantly, is there a workaround to allow require retrys?
Here's an example of what I've been trying:
function initialize() {
require(['socketio', function(io) {
io.connect('http://localhost');
app._bootstrap();
}, function(err) {
console.log(err);
setTimeout(initialize, 10000);
});
}
See Question&Answers more detail:os