Created
April 4, 2018 00:45
-
-
Save mitsuhiko/355d921bdde4ebded34bd2cf73cb4b18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let domain = require('domain'); | |
let fallbackCrumbs = []; | |
function getCrumbs() { | |
if (domain.active) { | |
let rv = domain.active.crumbs = domain.active.crumbs || []; | |
return rv; | |
} | |
return fallbackCrumbs; | |
} | |
function addCrumb(crumb) { | |
getCrumbs().push(crumb); | |
} | |
function showCrumbs(where) { | |
console.log(`crumbs from ${where}: ${getCrumbs().join('; ')}`); | |
} | |
let d = domain.create(); | |
d.enter(); | |
addCrumb('inner foo'); | |
setTimeout(() => { | |
addCrumb('inner bar'); | |
Promise.resolve(42).then(() => { | |
showCrumbs('inner'); | |
}); | |
}, 100); | |
d.exit(); | |
addCrumb('outer foo'); | |
showCrumbs('outer'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment