.val()
The.val() method is a convenience wrapper that simplifies reading data. It’s essentially shorthand for .once() but with a more intuitive name for getting values.
Signature
Parameters
function
Function called with the value once it’s loaded.Signature:
function(data, key)data- The value at this nodekey- The property key
object
Options passed through to
.once()Return Value
Returns a GUN chain reference for method chaining.Examples
Basic Usage
Why .val() Exists
Chaining after .val()
Implementation Details
The.val() method is not directly in the source files provided, but based on the GUN API patterns, it’s typically implemented as:
.once() that:
- Provides a more semantic name for “get value”
- Passes all arguments through to
.once() - Returns the same chain for method chaining
.val() vs .once() vs .on()
When to Use .val()
Experimental Status Warning
From the GUN documentation:- Change behavior in future versions
- Be renamed or removed
- Have different return values
- Gain new features
Recommended Alternative
For production code, use.once() instead:
Callback Context
Async/Await Pattern
Notes
.val()is experimental and may change- It’s essentially an alias for
.once()with a clearer name - Use
.once()for production code - Provides better semantics for “getting a value”
- Returns immediately; callback fires when data loads
- Automatically cleans up like
.once()does - Check GUN release notes for updates to this API