PromiseLogic.majority()

Composite Logic GateMAJORITY Logic - Succeeds when majority of Promises succeed

The PromiseLogic.majority() method implements MAJORITY (majority voting) logic gate semantics, succeeding when number of successful Promises exceeds to threshold. It returns a PromiseWithTimer instance which wraps native Promise and provides additional functionality.

Syntax

javascript
 PromiseLogic.majority(iterable, options?)

Parameters

ParameterTypeDescription
iterableIterable<Promise<T>>An iterable collection of Promise objects
options{ max?: number }Optional configuration object. max specifies success threshold (default 0.5, range 0-1)

Return Value

Returns a PromiseWithTimer that resolves to array of successful Promise results when majority threshold is met.

PromiseWithTimer<T[]>

Behavior

Success Condition

When number of successful Promises exceeds to threshold (default 0.5), PromiseWithTimer resolves to array of successful results.

Failure Condition

When number of successful Promises does not exceed threshold, PromiseWithTimer is rejected.

Error Types

MAJORITY_ERRORThrown when number of successful Promises does not exceed the threshold

Examples

Basic Usage

MAJORITY Logic Example - Succeeds when majority succeed

JAVASCRIPT
Loading...

Custom Threshold

MAJORITY Logic with custom threshold

JAVASCRIPT
Loading...

Error Handling

MAJORITY Logic Example - Majority not reached

JAVASCRIPT
Loading...

Timeout Control

Add timeout control with maxTimer method

JAVASCRIPT
Loading...

Real Application

Distributed system consensus with timeout

JAVASCRIPT
Loading...

Important Notes

  • MAJORITY logic requires number of successful Promises to exceed threshold (default 0.5)
  • Returns PromiseWithTimer which supports maxTimer() for timeout control
  • maxTimer can only detect timeout, cannot cancel Promise operation itself
  • Suitable for scenarios requiring majority agreement, such as distributed system consensus, voting systems
  • Custom threshold can be set via options.max parameter (range 0-1)
  • Returns array of successful Promise results when condition is met