The .once() method reads data from the graph once and calls the callback. Unlike .on(), it automatically unsubscribes after receiving data, making it ideal for one-time reads.
// Read user data oncegun.get('user/alice').once(function(data){ console.log('Alice:', data)})// Read a specific propertygun.get('user/alice').get('name').once(function(name){ console.log('Name:', name)})
// Promisify .once() for async/awaitfunction promiseOnce(gun) { return new Promise(function(resolve, reject){ gun.once(function(data){ resolve(data) }) })}// Use with async/awaitasync function getUser() { var user = await promiseOnce(gun.get('user/alice')) console.log('User:', user) return user}
// If data is a link, .once() resolves itgun.get('user/alice').get('car').once(function(car){ // car is the actual car data, not a link reference console.log('Color:', car.color)})// For the link itself, use immediate readvar carLink = gun.get('user/alice').get('car')._
The default wait time (99ms) determines when GUN assumes data doesn’t exist:
// Short wait for local/cached datagun.get('cached/data').once(function(data){ // Called within ~99ms})// Longer wait for remote/slow datagun.get('remote/data').once(function(data){ // Called within ~500ms or when data arrives}, { wait: 500 })