Skip to content

Instantly share code, notes, and snippets.

@dazld
Last active December 21, 2015 01:49
Show Gist options
  • Save dazld/6230730 to your computer and use it in GitHub Desktop.
Save dazld/6230730 to your computer and use it in GitHub Desktop.
Demonstrating race conditions using globals in an express app.
var express = require('express'),
app = express(),
userLoggedIn = false; // global shared between requests (just to show it not working, could be cookie based)
app.all('*', function(req, res, next) {
var id = Date.now();
userLoggedIn = Math.random() > 0.5 ? true : false; // imagine that this is based on some API call
console.log('Rx request id %s with userLoggedIn as %s', id, userLoggedIn);
var timer = Math.random() * 2500; // random response time
setTimeout(function() {
console.log('sending response id %s with userLoggedIn as %s', id, userLoggedIn);
var someResponse = userLoggedIn ? 'Hello User' : 'Not Logged In';
res.send(someResponse);
}, timer);
});
app.listen(8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment