Skip to content

Instantly share code, notes, and snippets.

@akhoury
Created May 20, 2016 17:26
Show Gist options
  • Save akhoury/acb852798e319b5ede93431e5910d3ef to your computer and use it in GitHub Desktop.
Save akhoury/acb852798e319b5ede93431e5910d3ef to your computer and use it in GitHub Desktop.
must require continuation-local-storage before others
/*
You must require CLS first,
In this example if you require request first, it closes over some dep before CLS could rewrite it
1. run it as it the first time.
2. then uncomment the first request and comment out the one at Line 15, then run it again.
*/
// var request = require('request');
var cls = require('continuation-local-storage');
var session = cls.createNamespace('session');
var request = require('request');
session.run(function() {
cls.getNamespace('session').set('foo', 'bar');
console.log("A", cls.getNamespace('session').get('foo'));
request({url: 'https://google.com'}, function() {
console.log("B", cls.getNamespace('session').get('foo'));
});
setTimeout(function() {
console.log("C", cls.getNamespace('session').get('foo'));
}, 3500);
setImmediate(function() {
console.log("D", cls.getNamespace('session').get('foo'));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment