Core Concepts

Theoretical FoundationUnderstand PromiseLogic's design philosophy and core concepts

PromiseLogic's core design philosophy is "Forget the API, remember the logic". By mapping complex Promise composition operations to intuitive logic gate semantics, asynchronous programming becomes more natural and easier to understand.

Design Philosophy

Logic Gate Semantic Mapping

Maps digital circuit logic gate concepts to Promise composition operations, allowing developers to understand asynchronous operations using familiar logical thinking.

javascript
// Traditional approach
const results = await Promise.all([promise1, promise2, promise3]);

// PromiseLogic approach
const results = await PromiseLogic.and([promise1, promise2, promise3]);

Semantic Consistency

Maintains full compatibility with standard Promise API while providing more intuitive semantic naming. Each logic gate operation has clear semantic meaning for easy memorization and usage.

javascript
// Standard Promise API
Promise.all()      → PromiseLogic.and()
Promise.race()     → PromiseLogic.race()
Promise.any()      → PromiseLogic.or()
Promise.allSettled() → PromiseLogic.allSettled()

Extensibility Design

Beyond standard logic gates, provides extended logic gate operations (XOR, NAND, NOR, etc.) to meet more complex business scenario requirements.

javascript
// Extended logic gate examples
const result = await PromiseLogic.xor([promise1, promise2]); // Exactly one succeeds
const result = await PromiseLogic.nand([promise1, promise2]); // At least one fails
const result = await PromiseLogic.majority([promise1, promise2, promise3]); // Majority succeeds

Core Concepts

Logic Gate Fundamentals

  • AND Logic: Output is true when all inputs are true
  • OR Logic: Output is true when at least one input is true
  • XOR Logic: Output is true when exactly one input is true
  • NOT Logic: Output is false when input is true, and vice versa

Promise Mapping

  • Success → Logical true (true)
  • Failure → Logical false (false)
  • Pending → Logical undetermined (pending)
  • Settled → Logical determined (settled)

Composition Operations

  • Parallel Execution: Launch multiple async operations simultaneously
  • Short-circuit Behavior: Terminate execution early under certain conditions
  • Result Aggregation: Combine multiple results into a single result
  • State Analysis: Analyze completion status of all operations

Error Handling

  • Immediate Failure: Fail entirely if any operation fails
  • Fault Tolerance: Allow partial operation failures
  • State Preservation: Preserve status information of all operations
  • Error Aggregation: Collect all failure reasons

Application Scenarios

Data Loading

Use PromiseLogic.and() to ensure all required data loads successfully, or use PromiseLogic.or() for service degradation strategies.

Timeout Control

Use PromiseLogic.race() to compete between requests and timeouts, implementing graceful timeout handling mechanisms.

Batch Operations

Use PromiseLogic.allSettled() to execute batch operations and analyze results, understanding the completion status of each operation.

Mutually Exclusive Operations

Use PromiseLogic.xor() to ensure exactly one operation succeeds, suitable for resource locking or uniqueness validation scenarios.

Best Practices

Choose Appropriate Logic Gates

Select appropriate logic gates based on business needs: use AND when all operations must succeed, use OR when partial failures are acceptable, use XOR for mutually exclusive operations.

Proper Error Handling

Wrap potentially failing operations with try-catch, or use allSettled to get all operation statuses, avoiding unhandled Promise rejections.

Performance Considerations

For large numbers of async operations, consider batch processing or concurrency control, avoiding performance issues from launching too many Promises simultaneously.

Code Readability

Use meaningful variable names, properly comment complex logic, maintain code clarity and maintainability.

Important Notes

  • PromiseLogic enhances the standard Promise API rather than replacing it
  • All methods return standard Promise objects and can integrate seamlessly with existing code
  • Logic gate semantics aim to improve code readability and maintainability
  • In real projects, choose appropriate naming conventions based on team standards