Skip to content
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

Result and undefined #69

Closed
vonagam opened this issue Jan 26, 2021 · 8 comments · Fixed by #70
Closed

Result and undefined #69

vonagam opened this issue Jan 26, 2021 · 8 comments · Fixed by #70

Comments

@vonagam
Copy link
Member

vonagam commented Jan 26, 2021

To check if result is set and if a function should be skipped an equality to undefined is used. But what if one wants to set result to undefined as a real return value?

Maybe "result" in ctx check should be used instead as a more strict rule? (And if you need to remove already set result from previous hooks, you'll do delete ctx.result.)

(Just a thought, nothing more.)

@daffl
Copy link
Member

daffl commented Jan 26, 2021

You should already be able to set it to null instead.

@vonagam
Copy link
Member Author

vonagam commented Jan 26, 2021

null is not undefined - they are not strictly equal and typescript won't even allow to assign null there.

@daffl
Copy link
Member

daffl commented Jan 26, 2021

The typing should be updated to allow null then since it should be possible. undefined is however not supported as a service return or hook result value.

@vonagam
Copy link
Member Author

vonagam commented Jan 26, 2021

But hooks are not only about services, aren't they?

Well if the official position is that undefined should not be supported as a possible return value of a function - then this can be closed.

@daffl
Copy link
Member

daffl commented Jan 26, 2021

That's fair. One option would be to add an UNDEFINED symbol that you can set the result to in https://github.com/feathersjs/hooks so it would be possible going forward.

@vonagam
Copy link
Member Author

vonagam commented Jan 26, 2021

Yeah, thought about it (symbol). But it creates problems when you work with arbitrary functions and set their result to result property. So now you'll need to replace their undefined with UNDEFINED and if you access context.result you need to remember about UNDEFINED and so on... (and I'm sure nobody will remember)

@vonagam
Copy link
Member Author

vonagam commented Jan 26, 2021

It creates problems when undefined can mean both things - actual return value and absence of a value.

In feathers ecosystem you control rules and you can ban undefined as a return value of services, solving the ambiguity.

But when it is about random functions (many of which will be void, so should return only undefined) - i don't think you can make it work without opening door for bugs...

I assume that you don't like the idea because of breaking nature, right? Or is there anything else against it?

@daffl
Copy link
Member

daffl commented Feb 3, 2021

You're right, this is a good suggestion and I don't think it would be a breaking change.

I just created a pull request in #70 that should allow to do what you are suggesting by checking if there is a property descriptor for context.result. That way if context.result is ever set to anything at all in the hook chain, the method call will be skipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants