- What are domains?
- How restify uses domains
- People have been having problems using Promises with restify
- And people have been having problems with Promises and domains in node for a long time
- Maybe it will be resolved in node v8.x
- In the meantime, one "solution" (a/k/a work-around, a/k/a hack) is to swap out the native Promise for a bluebird promise 😱
FAQ
Q: What should I do?
A: Every Restify 4.x app should use bluebird
instead of native Promises.
Q: How do I do that?
A: At the beginning of your app (where you load dotenv
) you should add the following:
global.Promise = require("bluebird");
Q: Awesome! 🎉 Now I can just use all the neat-o bluebird promise methods!
A: That is not a question. Also, please don't. Please treat every Promise in your app like a native Promise and don't invoke bluebird methods without explicity using bluebird. If you fail to do that, it will be nearly impossible to undo this hack revert this temporary (albeit clever) solution. So, to be clear:
// bad
return new Promise((resove, reject) => {
// ...
if (err) {
reject(err);
}
else {
resolve(true);
}
})
.finally(() => {
// native promise has no method `finally`
console.log("finished");
});
// good
const bluebird = require("bluebird");
return new bluebird((resove, reject) => {
// ...
if (err) {
reject(err);
}
else {
resolve(true);
}
})
.finally(() => {
// 👌
console.log("finished");
});