-
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Hemera.add should throw BusinessError if promise-based handler rejects #162
Comments
In the example above asCallback is from https://www.npmjs.com/package/ascallback |
If I understand your case correctly you want to support to pass promises to callback handler or return them inside a normal function? When yes this is definitely wrong because there are only two types of asynchronous composition which are very common in javascript.
hemera.add({
topic: 'test',
cmd: 'boom',
fail: Joi.boolean().required(),
}, function (req, cb) {
cb(<error>, <result>) // result can be any value, error must be an instance of Error
})
hemera.add({
topic: 'test',
cmd: 'boom',
fail: Joi.boolean().required(),
}, async function (req, cb) {
const result = await Promise.resolve()
return result // can be a promise or any other value
})
Some others support this syntax but we don't provide a reply interface in an hemera.add({
topic: 'test',
cmd: 'boom',
fail: Joi.boolean().required(),
}, async function (req, reply) {
reply.send(Promise.resolve())
}) This is possible in hemera. hemera
.act(
{
topic: 'math',
cmd: 'add',
a: 1,
b: 2
})
.then(..)
.catch(..) Could I answer your question? |
You have to decide between callback or async / await don't mix them. |
Yep, if you want to use promises with hemera, return an async function or use asCallback. |
Could I answer your question? |
Yes, I believe so. Thanks. I've always been under the impression that (from the callers perspective) both of the following just return a promise. I guess there are some subtleties that I haven't ran into until now.
|
No, this is quite uncommon. |
Description
It would be nice if returning a promise from a Hemera.add handler worked the same as using a callback. For example, I would expect both services below to behave identically.
Steps to Reproduce
cd hemera-add-promise-test
yarn install
npm start
Expected Result
The catch should immediately show a BusinessError
Actual Result
The console shows a deprecation warning for an unhandled promise rejection and the catch shows a Timeout error after the timeout expires.
Context
Your Environment
The text was updated successfully, but these errors were encountered: