Skip to content

Instantly share code, notes, and snippets.

@lukashavrlant
Created June 23, 2017 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukashavrlant/6a4775efbb09e24c2eaca58a1fdd724f to your computer and use it in GitHub Desktop.
Save lukashavrlant/6a4775efbb09e24c2eaca58a1fdd724f to your computer and use it in GitHub Desktop.
const continuation = require('continuation-local-storage');
const uuid = require('node-uuid');
const NAMESPACE = 'luha-test';
const namespace = continuation.createNamespace(NAMESPACE);
for (let i = 0; i < 5; i++) {
namespace.run(function () {
const id = uuid.v4();
namespace.set('correlationId', id);
setTimeout(() => printCorrelationId(id), Math.random() * 3000);
});
}
function printCorrelationId(generatedId) {
const ns = continuation.getNamespace(NAMESPACE);
const correlationId = ns.get('correlationId');
console.log(`Passed correlation Id is ${generatedId}, continuation correlation Id is ${correlationId}. Are they equal? ${generatedId === correlationId}`);
new Promise(function (resolve) {
setTimeout(resolve, Math.random() * 3000);
}).then(function () {
printCorrelationId(generatedId);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment