Skip to main content

.back()

The .back() method traverses up the chain to parent references. It’s useful for navigating back after descending into nested properties or returning to the root database.

Signature

Parameters

number
Number of levels to go back. Special values:
  • 1 (default): Go back one level
  • -1: Go to root
  • Infinity: Go to root
  • 2, 3, etc.: Go back multiple levels
string | array
Property path to traverse. Can be:
  • String with dots: 'opt.peers'
  • Array of keys: ['opt', 'peers']
Returns the value at that path in the chain metadata.
function
Function called for each parent until it returns a value.Signature: function(parent, opt)
  • parent - Parent chain context
  • opt - Options passed through
Return: Stop traversing and return this value

Return Value

Returns a GUN chain reference to the parent (or root) for method chaining.

Examples

Go Back One Level

Return to Root

Multiple Levels

Access Metadata Path

Traverse with Callback

Build Path String

Implementation Details

From the source code (back.js:4-39):
The .back() method:
  1. With no args or 1: goes back one level
  2. With -1 or Infinity: returns root
  3. With string/array: accesses chain metadata
  4. With callback: traverses until callback returns a value
  5. With number > 1: recursively goes back N levels

Chain Hierarchy

Root Access Pattern

Metadata Access

Notes

  • .back() with no args goes back one level
  • .back(-1) and .back(Infinity) return the root
  • Useful for complex navigation patterns
  • Returns immediately (synchronous)
  • The callback version traverses until it gets a return value
  • Path access is useful for getting configuration from the root
  • Each chain maintains a reference to its parent via ._back
  • Cannot go “forward” - only up the chain