PromiseLogic.xor()

Core Logic GateXOR Logic - Succeeds when exactly one Promise succeeds

The PromiseLogic.xor() method implements XOR (exclusive OR) logic gate semantics, succeeding when exactly one Promise succeeds. It returns a PromiseWithTimer instance which wraps native Promise and provides additional functionality.

Syntax

javascript
 PromiseLogic.xor(iterable)

Parameters

ParameterTypeDescription
iterableIterable<Promise<T>>An iterable collection of Promise objects

Return Value

Returns a PromiseWithTimer that resolves to value of successful Promise when exactly one Promise succeeds.

PromiseWithTimer<T>

Behavior

Success Condition

When exactly one Promise resolves successfully, PromiseWithTimer resolves to that Promise's value.

Failure Condition

When no Promise succeeds or more than one Promise succeeds, PromiseWithTimer is rejected.

Error Types

XOR_ERRORThrown when the number of successful Promises is not exactly one

Examples

Basic Usage

XOR Logic Example - Succeeds when exactly one succeeds

JAVASCRIPT
Loading...

Error Handling

XOR Logic Failure Example - Multiple Promises succeed

JAVASCRIPT
Loading...

Timeout Control

Add timeout control with maxTimer method

JAVASCRIPT
Loading...

Real Application

Primary-backup service pattern with timeout

JAVASCRIPT
Loading...

Important Notes

  • XOR logic requires exactly one Promise to succeed, otherwise it is considered a failure
  • Returns PromiseWithTimer which supports maxTimer() for timeout control
  • maxTimer can only detect timeout, cannot cancel Promise operation itself
  • Suitable for scenarios requiring exclusive operations, such as resource locking, uniqueness validation
  • When no Promise succeeds, returns all Promise rejection reasons
  • When multiple Promises succeed, returns an error indicating multiple operations succeeded simultaneously