Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const cls = require('cls-hooked');
- const ctx = cls.createNamespace('test-cls-context');
- // result of this demo: 4 and 13
- // despite ctx is "global", every async "context" has it's own strage and values are not mixed between timeouts
- setTimeout(() => {
- ctx.run(() => {
- ctx.set('value', 1);
- setTimeout(() => {
- ctx.set('value', ctx.get('value') + 1);
- setTimeout(() => {
- ctx.set('value', ctx.get('value') + 2);
- process.nextTick(() => {
- console.log(ctx.get('value'));
- })
- })
- })
- });
- });
- setTimeout(() => {
- ctx.run(() => {
- ctx.set('value', 10);
- setTimeout(() => {
- ctx.set('value', ctx.get('value') + 1);
- setTimeout(() => {
- ctx.set('value', ctx.get('value') + 2);
- process.nextTick(() => {
- console.log(ctx.get('value'));
- })
- })
- })
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement