Created
March 31, 2017 01:08
-
-
Save owenallenaz/a24beab8db317094de53d2325d7cf94e to your computer and use it in GitHub Desktop.
Race condition using require and loader plugins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.3/require.min.js"></script> | |
<script> | |
require.config({ | |
paths : { | |
"slow" : "/slow", | |
}, | |
waitSeconds : 60 | |
}); | |
define("load", function() { | |
return { | |
load : function(name, req, onload, config) { | |
require([name], function(module) { | |
onload(module); | |
}); | |
} | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Testing</h1> | |
<script> | |
console.log("script 1"); | |
require(["slow"], function() { | |
console.log("script 1 queue"); | |
}); | |
require(["load!slow"], function() { | |
console.log("script 1 using"); | |
}); | |
</script> | |
<script src="/slow_standard.js"></script> | |
<script> | |
console.log("script 2"); | |
require(["slow"], function() { | |
console.log("script 2 queue"); | |
}); | |
require(["load!slow"], function() { | |
console.log("script 2 using"); | |
}); | |
</script> | |
<script> | |
console.log("script 3"); | |
require(["slow"], function() { | |
console.log("script 3 queue"); | |
}); | |
require(["load!slow"], function() { | |
console.log("script 3 using"); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"); | |
var fs = require("fs"); | |
var app = express(); | |
app.use("/static", express.static("static")); | |
app.get("/", function(req, res) { | |
var html = fs.readFileSync(__dirname + "/index.html"); | |
res.send(html.toString()); | |
}); | |
app.get("/slow.js", function(req, res) { | |
var duration = req.query.duration || 1000; | |
setTimeout(function() { | |
res.send(` | |
define([], function() { | |
console.log('slow loaded'); | |
}) | |
`); | |
}, duration); | |
}); | |
app.get("/slow_standard.js", function(req, res) { | |
var duration = req.query.duration || 1000; | |
setTimeout(function() { | |
res.send(` | |
console.log("done"); | |
`); | |
}, duration); | |
}); | |
app.listen(80, function() { | |
console.log("booted"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment