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.
The .val() method is experimental. Its behavior and API may change in future versions. Use .once() for production code if you need stable behavior.
// Read a value (same as .once())gun.get('user/alice').get('name').val(function(name){ console.log('Name:', name)})// Equivalent to:gun.get('user/alice').get('name').once(function(name){ console.log('Name:', name)})
// More intuitive naming for getting valuesgun.get('settings').get('theme').val(function(theme){ applyTheme(theme)})// Reads clearer than .once() in some contextsgun.get('score').val(function(score){ updateScoreboard(score)})
// ✅ Good use cases:// Quick value readsgun.get('config').get('apiKey').val(cb)// Form populationgun.get('user').get('email').val(function(email){ document.getElementById('email').value = email})// ❌ Use .once() instead for:// Production code (more stable)gun.get('critical/data').once(cb)// ❌ Use .on() instead for:// Realtime updatesgun.get('live/data').on(cb)
// This warning appears when using .val()Gun.log.once("valonce", "Chainable val is experimental, its behavior and API may " + "change moving forward. Please play with it and report bugs " + "and ideas on how to improve it.")
// Promisify .val() for async/awaitfunction promiseVal(gun) { return new Promise(function(resolve, reject){ gun.val(function(data){ resolve(data) }) })}// Use with async/awaitasync function getData() { var value = await promiseVal(gun.get('data')) console.log('Value:', value) return value}