Quick Start
Welcome to PromiseLogic! This guide will help you quickly master how to use logic gate semantics to compose Promises through a series of practical examples. Whether you're a beginner or an experienced developer, these examples will help you understand PromiseLogic's core concepts.
All PromiseLogic methods return a PromiseWithTimer instance which wraps native Promise and provides additional functionality like timeout control via the maxTimer() method.
Prerequisites
Before You Start
- • Ensure PromiseLogic is properly installed following Installation Guide
- • Familiar with basic JavaScript Promise concepts
- • Understand fundamentals of asynchronous programming
- • Have your development environment ready (Node.js or browser)
Core Concepts Overview
AND Logic
Succeeds when all Promises succeed, equivalent to standard Promise.all()
OR Logic
Returns first successful Promise, equivalent to Promise.any()
RACE Logic
Returns first completed Promise, equivalent to Promise.race()
ALLSETTLED Logic
Waits for all Promises to complete, equivalent to Promise.allSettled()
PromiseWithTimer
Timeout Control
All PromiseLogic methods return a PromiseWithTimer instance which provides the maxTimer() method for timeout control.
// Use maxTimer to add timeout control
const result = await PromiseLogic.and([
fetch('/api/data1').then(r => r.json()),
fetch('/api/data2').then(r => r.json())
]).maxTimer(5000) // 5 second timeout
// If timeout occurs, an error will be thrown
Note: maxTimer can only detect timeout, cannot cancel Promise operation itself
Basic Examples
The following examples showcase PromiseLogic's most commonly used logic gate operations. Each example can be directly copied and run in your project.
Basic AND Logic
Succeeds when all Promises succeed, equivalent to Promise.all()
Timeout Control
Use maxTimer to add timeout control
Basic OR Logic
Returns first successful Promise, equivalent to Promise.any()
Basic RACE Logic
Returns first completed Promise (regardless of success or failure), equivalent to Promise.race()
Basic ALLSETTLED Logic
Waits for all Promises to complete (regardless of success or failure), equivalent to Promise.allSettled()
Composite Logic Application
Combine multiple logic gates to implement complex business logic
Next Steps
Deepen Your Learning
- • Check AND Logic Documentation for detailed usage
- • Explore OR Logic Documentation to learn service degradation strategies
- • Learn XOR Logic to implement mutually exclusive operations
- • Understand ALLSETTLED Logic for result analysis
Quick Start Tips
- • All examples can be run directly in browser console or Node.js environment
- • Recommended to understand basic examples before trying composite logic applications
- • Remember to add appropriate error handling in real projects
- • PromiseLogic is fully compatible with existing Promise code and can be migrated gradually
- • Use maxTimer() to add timeout control to any PromiseLogic operation
- • If you encounter issues, refer to detailed API documentation or submit an Issue