Resolve
ixfx has a set of functions for ‘resolving’ something to a concrete value.
This can be useful for handling different kinds of input. Let’s say our function blah can process numbers:
But we also want it to accept numbers from asynchronous sources, generators, reactive and so on. Rather than writing all that code, Data.resolve can be used. Now our function can take all sorts of things that can eventually be resolved to a number.
Data.resolveFields has the same logic as resolve
, but it works across all the top-level properties of an object.
For example, let us say we have some state which is a combination of primitive data and functions which compute data dynamically:
Ideally the use
function doesn’t need to care about how the data is being computed. But now we have to execute the random
function and keep track of another variable. Instead resolveFields
can be used.
Now our use
function doesn’t need to care about how properties are being computed or if even if they are dynamic or not. We can swap length
from being a number to a function and use
doesn’t need to change.
More
resolve
alternatives:
- Data.resolveSync - for synchronous sources
- Data.resolveWithFallback - to return a fallback value if
resolve
returns undefined or an error is thrown - Data.resolveWithFallbackSync - as above, but for synchronous sources