Last active
May 19, 2017 14:01
Domains & Promises
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
var domain = require('domain'); | |
var d = domain.create(); | |
// Example 1: callback style | |
d.run(() => { | |
console.log('Initial domain is', process.domain); | |
setTimeout(() => { | |
console.log('Final domain is', process.domain); // good | |
}, 1000); | |
}); | |
// Example 2: promises | |
d.run(() => { | |
console.log('Initial domain is', process.domain); | |
Promise.resolve('foo').then(() => { | |
console.log('Final domain is', process.domain); // undefined! | |
}); | |
}); | |
// Example 3: Bluebird promises (tested with 3.5.0) | |
var Promise = require('bluebird'); | |
d.run(() => { | |
console.log('Initial domain is', process.domain); | |
Promise.resolve('foo').then(() => { | |
console.log('Final domain is', process.domain); // good | |
}); | |
}); | |
// Solution: global.Promise = require('bluebird'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment