Proposal: If input is function, call it to use return value as data #801
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey! I am not sure if you are currently considering feature proposals. This is just a small enhancement that I wish
TrackedAsyncData
would support. Happy for any reviews/suggestions/support to get this merged. However, feel free to disregard if you think this is out-of-scope! :)Motivation
We use
TrackedAsyncData
(222 results in 63 files), mostly to fetch data on mount of a component:We use a variety of patterns when the business logic becomes more complex, for example, returning different
TrackedAsyncData
for each conditional path, then-chaining additional logic on the initial promise, or wrapping everything in anew Promise
.TrackedAsyncData
return values:new Promise
:All of these work perfectly fine. However, I wish you could pass
TrackedAsyncData
an inline callback function directly without the need to wrap it in anew Promise
or usethen
chaining:Now, I have a co-located inline/nested function that can easily be moved out to be a private helper or moved to a service without needing any more refactoring. It's also super easy to read without any wrapping, then-chaining, or similar! Would love this to be a thing!
Proposed changes
Check
data
TrackedAsyncData
constructor argument. If it is a function, call it to use the return value as the data.This is technically a breaking change because currently a passed-in function is used as the value (this actually happened to me on accident before). I can't think of a use case where this would be the desired behavior (have a function as a value of a
TrackedAsyncData
object), but that behavior would technically break now. I updated thepackage.json
version tov2
because of this, but I also think you have existing plans for av2
update already. Happy to merge this into av2
branch or follow any other recommendations!Tests
I added basic tests and type tests for the changes. Please let me know if I should add more.
Considerations
I think it would be reasonable to support this case but also would understand if you think the added scope and complexity is not worth adding this. Thank you for taking a look!