Skip to content

Instantly share code, notes, and snippets.

@rubennorte
Last active May 19, 2017 14:01
Show Gist options
  • Save rubennorte/f10d2f6ec376b28589a98bb17ef6ae85 to your computer and use it in GitHub Desktop.
Save rubennorte/f10d2f6ec376b28589a98bb17ef6ae85 to your computer and use it in GitHub Desktop.
Domains & Promises
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