Runs a callback asynchronously, but outside of current CLS context.
callback
is called in next tick. As opposed to setImmediate
, process.nextTick()
etc, the continuation-local-storage context is not maintained.
Primarily, this module exists to facilitate testing of node.js libraries / applications that use continuation-local-storage.
This module is a lightweight way to test what happens when CLS context is lost (which many libraries e.g. redis
do) and to ensure that your own code handles this correctly, perhaps by re-binding with namespace.bind()
.
var cls = require('continuation-local-storage');
var namespace = cls.createNamespace('test');
var loseCls = require('lose-cls-context');
namespace.run(function() {
namespace.set('value', 123);
setImmediate(function() {
var value = namespace.get('value');
// CLS context is maintained - value === 123
});
loseCls(function() {
var value = namespace.get('value');
// CLS context has been lost - value === undefined
});
});
Use npm test
to run the tests. Use npm run cover
to check coverage.
See changelog.md
If you discover a bug, please raise an issue on Github. https://github.com/overlookmotel/lose-cls-context/issues
Pull requests are very welcome. Please:
- ensure all tests pass before submitting PR
- add an entry to changelog
- add tests for new features
- document new functionality/API additions in README