promise-logic 2.4.3 → 2.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -263
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,300 +1,136 @@
|
|
|
1
1
|
|
|
2
|
-
# Promise Logic - Advanced Promise Logic Gates for Async Programming
|
|
3
2
|
|
|
4
|
-
Compose promises with logic gate semantics (AND, OR, NOT, XOR, NAND, NOR, XNOR, Majority). Forget APIs, remember logic.
|
|
5
3
|
|
|
6
|
-
|
|
4
|
+
### **1. Core Philosophy**
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
- **
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
- **Zero Dependencies**: Pure JavaScript/TypeScript implementation
|
|
13
|
-
- **Tree Shakeable**: Optimized for modern bundlers
|
|
6
|
+
**Replace API Memory with Logical Concepts**
|
|
7
|
+
The design philosophy of `promise-logic` is: **Developers should focus on business logic, not the details of Promise APIs**.
|
|
8
|
+
Traditional Promise combinations (such as `Promise.all`, `Promise.race`) have naming and semantics that are not intuitive enough, especially in complex asynchronous scenarios where code readability rapidly declines.
|
|
9
|
+
`promise-logic` abstracts asynchronous combinations into logical operations like `AND`, `OR`, `XOR` through the concept of **Logic Gates**, making code semantically clear and self-explanatory.
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
---
|
|
16
12
|
|
|
17
|
-
###
|
|
13
|
+
### **2. Features**
|
|
18
14
|
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
1. **Logical Semantics**
|
|
16
|
+
- `AND`: All tasks must succeed (equivalent to `Promise.all`)
|
|
17
|
+
- `OR`: At least one task succeeds (equivalent to `Promise.race`)
|
|
18
|
+
- `XOR`: **Exactly one task succeeds** (no direct equivalent in traditional Promise)
|
|
19
|
+
- `NAND`: All tasks fail
|
|
20
|
+
- `Majority`: Most tasks succeed
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
await PromiseLogic.not(Promise.resolve('success')); // Rejects with 'success'
|
|
25
|
-
|
|
26
|
-
// Failure -> Success transformation
|
|
27
|
-
const result = await PromiseLogic.not(Promise.reject('error')); // Resolves with 'error'
|
|
28
|
-
```
|
|
22
|
+
2. **Zero Dependencies**
|
|
23
|
+
Only depends on native Promise, no additional runtime dependencies.
|
|
29
24
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
- Full generic type propagation
|
|
33
|
-
- Strict type checking with zero `any` types
|
|
34
|
-
- Advanced type inference for all operations
|
|
35
|
-
- Seamless IDE integration with IntelliSense
|
|
25
|
+
3. **Full Test Coverage**
|
|
26
|
+
All logic gates have undergone rigorous unit testing to ensure behavior meets expectations.
|
|
36
27
|
|
|
37
|
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
- Improved error handling and edge case management
|
|
28
|
+
4. **Clear Error Classification**
|
|
29
|
+
- `PromiseLogicError` unified error type
|
|
30
|
+
- `error.type` distinguishes specific logical errors (e.g., `'XOR_ERROR'`)
|
|
41
31
|
|
|
42
|
-
|
|
43
|
-
- Comprehensive API reference with TypeScript examples
|
|
44
|
-
- Better usage guidelines and best practices
|
|
45
|
-
- Clear entry point documentation for different environments
|
|
32
|
+
---
|
|
46
33
|
|
|
47
|
-
|
|
34
|
+
### **3. Installation**
|
|
48
35
|
|
|
49
36
|
```bash
|
|
50
37
|
npm install promise-logic
|
|
51
38
|
```
|
|
52
39
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
### JavaScript (Default)
|
|
56
|
-
|
|
57
|
-
```javascript
|
|
58
|
-
// ES Modules
|
|
59
|
-
import { PromiseLogic } from 'promise-logic';
|
|
60
|
-
|
|
61
|
-
// CommonJS
|
|
62
|
-
const { PromiseLogic } = require('promise-logic');
|
|
63
|
-
|
|
64
|
-
// Use logic gates
|
|
65
|
-
const results = await PromiseLogic.and([
|
|
66
|
-
Promise.resolve('data1'),
|
|
67
|
-
Promise.resolve('data2')
|
|
68
|
-
]);
|
|
69
|
-
// results = ['data1', 'data2']
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### TypeScript (Enhanced)
|
|
73
|
-
|
|
74
|
-
```typescript
|
|
75
|
-
// TypeScript version with full type inference
|
|
76
|
-
import { PromiseLogic } from 'promise-logic/typescript';
|
|
77
|
-
|
|
78
|
-
// Type-safe operations with automatic inference
|
|
79
|
-
const numbers = await PromiseLogic.and<number>([
|
|
80
|
-
Promise.resolve(1),
|
|
81
|
-
Promise.resolve(2)
|
|
82
|
-
]);
|
|
83
|
-
|
|
84
|
-
const strings = await PromiseLogic.or<string>([
|
|
85
|
-
Promise.resolve('hello'),
|
|
86
|
-
Promise.resolve('world')
|
|
87
|
-
]);
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
## Core Logic Gates
|
|
91
|
-
|
|
92
|
-
### `and(promises)`
|
|
93
|
-
Resolves with all values when all promises fulfill. Equivalent to `Promise.all()`.
|
|
94
|
-
|
|
95
|
-
```javascript
|
|
96
|
-
const results = await PromiseLogic.and([
|
|
97
|
-
Promise.resolve(1),
|
|
98
|
-
Promise.resolve(2),
|
|
99
|
-
Promise.resolve(3)
|
|
100
|
-
]);
|
|
101
|
-
// results = [1, 2, 3]
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
### `or(promises)`
|
|
105
|
-
Resolves with the first fulfilled promise. Equivalent to `Promise.any()`.
|
|
106
|
-
|
|
107
|
-
```javascript
|
|
108
|
-
const result = await PromiseLogic.or([
|
|
109
|
-
Promise.reject('error'),
|
|
110
|
-
Promise.resolve('success')
|
|
111
|
-
]);
|
|
112
|
-
// result = 'success'
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### `xor(promises)`
|
|
116
|
-
Exclusive OR - resolves only when exactly one promise fulfills.
|
|
117
|
-
|
|
118
|
-
```javascript
|
|
119
|
-
try {
|
|
120
|
-
const result = await PromiseLogic.xor([
|
|
121
|
-
Promise.reject('error1'),
|
|
122
|
-
Promise.resolve('success'),
|
|
123
|
-
Promise.reject('error2')
|
|
124
|
-
]);
|
|
125
|
-
// result = 'success'
|
|
126
|
-
} catch (error) {
|
|
127
|
-
// Throws if zero or multiple promises fulfill
|
|
128
|
-
}
|
|
129
|
-
```
|
|
40
|
+
---
|
|
130
41
|
|
|
131
|
-
###
|
|
132
|
-
Not AND - resolves when not all promises fulfill.
|
|
42
|
+
### **4. Quick Start**
|
|
133
43
|
|
|
44
|
+
#### Example: Primary/Backup Service Call (XOR Scenario)
|
|
134
45
|
```javascript
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
//
|
|
46
|
+
import { XOR } from 'promise-logic';
|
|
47
|
+
|
|
48
|
+
// Primary service call
|
|
49
|
+
const primary = fetch('https://api.main.com/data');
|
|
50
|
+
// Backup service call
|
|
51
|
+
const backup = fetch('https://api.backup.com/data');
|
|
52
|
+
|
|
53
|
+
// Execute XOR logic: exactly one success
|
|
54
|
+
XOR(primary, backup)
|
|
55
|
+
.then(result => {
|
|
56
|
+
console.log('Successfully fetched data:', result);
|
|
57
|
+
})
|
|
58
|
+
.catch(error => {
|
|
59
|
+
if (error.type === 'XOR_ERROR') {
|
|
60
|
+
console.error('Both primary and backup services succeeded or failed, which does not meet XOR semantics');
|
|
61
|
+
} else {
|
|
62
|
+
console.error('Network error:', error);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
140
65
|
```
|
|
141
66
|
|
|
142
|
-
|
|
143
|
-
Not OR - resolves only when all promises reject.
|
|
144
|
-
|
|
67
|
+
#### Example: Majority Decision (Majority Scenario)
|
|
145
68
|
```javascript
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
69
|
+
import { Majority } from 'promise-logic';
|
|
70
|
+
|
|
71
|
+
const services = [
|
|
72
|
+
fetch('https://api.node1.com/vote'),
|
|
73
|
+
fetch('https://api.node2.com/vote'),
|
|
74
|
+
fetch('https://api.node3.com/vote')
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
Majority(...services)
|
|
78
|
+
.then(results => {
|
|
79
|
+
console.log('Majority of services returned success:', results);
|
|
80
|
+
})
|
|
81
|
+
.catch(error => {
|
|
82
|
+
console.error('Majority of services failed:', error);
|
|
83
|
+
});
|
|
151
84
|
```
|
|
152
85
|
|
|
153
|
-
|
|
154
|
-
Exclusive NOR - resolves when all promises have the same outcome.
|
|
86
|
+
---
|
|
155
87
|
|
|
156
|
-
|
|
157
|
-
const results = await PromiseLogic.xnor([
|
|
158
|
-
Promise.resolve('a'),
|
|
159
|
-
Promise.resolve('b')
|
|
160
|
-
]);
|
|
161
|
-
// results = ['a', 'b']
|
|
162
|
-
```
|
|
88
|
+
### **5. API Reference**
|
|
163
89
|
|
|
164
|
-
|
|
165
|
-
|
|
90
|
+
| API | Description |
|
|
91
|
+
| :--------- | :-------------------------------------------------------------------------- |
|
|
92
|
+
| `AND` | All Promises succeed, returns result array; any failure causes overall failure. |
|
|
93
|
+
| `OR` | At least one Promise succeeds, returns first success result; all failures cause overall failure. |
|
|
94
|
+
| `XOR` | **Exactly one Promise succeeds**, returns that result; otherwise throws `XOR_ERROR`. |
|
|
95
|
+
| `NAND` | All Promises fail, returns error array; any success causes overall failure. |
|
|
96
|
+
| `Majority` | More than half of Promises succeed, returns success result array; otherwise overall failure. |
|
|
166
97
|
|
|
167
|
-
|
|
168
|
-
const results = await PromiseLogic.majority([
|
|
169
|
-
Promise.resolve('a'),
|
|
170
|
-
Promise.resolve('b'),
|
|
171
|
-
Promise.reject('error')
|
|
172
|
-
]);
|
|
173
|
-
// results = ['a', 'b']
|
|
174
|
-
```
|
|
98
|
+
---
|
|
175
99
|
|
|
176
|
-
|
|
100
|
+
### **6. Real-world Application Scenarios**
|
|
177
101
|
|
|
178
|
-
|
|
179
|
-
|
|
102
|
+
1. **Primary/Backup Service Calls**
|
|
103
|
+
- Use `XOR` to ensure **exactly one service responds**, avoiding duplicate processing.
|
|
104
|
+
2. **Distributed Decision Making**
|
|
105
|
+
- Use `Majority` to implement majority consensus (e.g., distributed voting).
|
|
106
|
+
3. **Resource Competition**
|
|
107
|
+
- Use `OR` to get the first available resource (e.g., CDN node selection).
|
|
108
|
+
4. **Full-link Validation**
|
|
109
|
+
- Use `AND` to ensure all dependent services succeed (e.g., order creation).
|
|
180
110
|
|
|
181
|
-
|
|
182
|
-
// Success -> Failure
|
|
183
|
-
try {
|
|
184
|
-
await PromiseLogic.not(Promise.resolve('success'));
|
|
185
|
-
} catch (error) {
|
|
186
|
-
console.log(error); // 'success'
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Failure -> Success
|
|
190
|
-
const result = await PromiseLogic.not(Promise.reject('error'));
|
|
191
|
-
console.log(result); // 'error'
|
|
192
|
-
```
|
|
111
|
+
---
|
|
193
112
|
|
|
194
|
-
**
|
|
195
|
-
- Transform error handling patterns
|
|
196
|
-
- Create conditional promise flows
|
|
197
|
-
- Implement retry logic with inverted conditions
|
|
198
|
-
- Build fallback mechanisms
|
|
113
|
+
### **7. Contribution Guide**
|
|
199
114
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
### `createFlipFlop(initialState?)`
|
|
215
|
-
Creates a stateful flip-flop for managing boolean state across async operations.
|
|
216
|
-
|
|
217
|
-
```javascript
|
|
218
|
-
const flipFlop = PromiseLogic.createFlipFlop(false);
|
|
219
|
-
|
|
220
|
-
// Get current state
|
|
221
|
-
console.log(flipFlop.getState()); // false
|
|
222
|
-
|
|
223
|
-
// Toggle state
|
|
224
|
-
await flipFlop.toggle();
|
|
225
|
-
console.log(flipFlop.getState()); // true
|
|
226
|
-
|
|
227
|
-
// Wait for specific state
|
|
228
|
-
await flipFlop.waitFor(true); // Resolves immediately if already true
|
|
229
|
-
|
|
230
|
-
// Async state change
|
|
231
|
-
setTimeout(() => flipFlop.set(false), 100);
|
|
232
|
-
await flipFlop.waitForChange(); // Waits for state change
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
## TypeScript Support - Enhanced in v2.3.2
|
|
236
|
-
|
|
237
|
-
The TypeScript version (`promise-logic/typescript`) provides:
|
|
238
|
-
|
|
239
|
-
- **Full Type Inference**: Automatic type deduction for all operations
|
|
240
|
-
- **Strict Type Checking**: Zero `any` types, complete type safety
|
|
241
|
-
- **IDE Support**: Enhanced IntelliSense and code completion
|
|
242
|
-
- **Generic Type Propagation**: Proper handling of complex generic scenarios
|
|
243
|
-
|
|
244
|
-
```typescript
|
|
245
|
-
import { PromiseLogic } from 'promise-logic/typescript';
|
|
246
|
-
|
|
247
|
-
// TypeScript infers everything
|
|
248
|
-
const result = await PromiseLogic.and([
|
|
249
|
-
Promise.resolve({ id: 1, name: 'Alice' }),
|
|
250
|
-
Promise.resolve({ id: 2, name: 'Bob' })
|
|
251
|
-
]);
|
|
252
|
-
// result type: Array<{ id: number, name: string }>
|
|
253
|
-
|
|
254
|
-
// Complex generic types work seamlessly
|
|
255
|
-
async function processPromises<T>(promises: Promise<T>[]): Promise<T[]> {
|
|
256
|
-
return await PromiseLogic.and(promises);
|
|
257
|
-
}
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
## Entry Points
|
|
261
|
-
|
|
262
|
-
| Import Path | Purpose | Recommended For |
|
|
263
|
-
|------------|---------|----------------|
|
|
264
|
-
| `promise-logic` | Default JavaScript version | General use, mixed codebases |
|
|
265
|
-
| `promise-logic/typescript` | Enhanced TypeScript version | TypeScript projects, strict type safety |
|
|
266
|
-
|
|
267
|
-
## Migration Note
|
|
268
|
-
|
|
269
|
-
If you're upgrading from earlier versions, note that the TypeScript system has been completely redesigned for better type safety and developer experience. All existing APIs remain compatible.
|
|
270
|
-
|
|
271
|
-
## Development
|
|
272
|
-
|
|
273
|
-
```bash
|
|
274
|
-
# Install dependencies
|
|
275
|
-
npm install
|
|
276
|
-
|
|
277
|
-
# Build the project
|
|
278
|
-
npm run build
|
|
279
|
-
|
|
280
|
-
# Run tests
|
|
281
|
-
npm test
|
|
282
|
-
|
|
283
|
-
# Run tests in watch mode
|
|
284
|
-
npm run test:watch
|
|
285
|
-
|
|
286
|
-
# Check code coverage
|
|
287
|
-
npm run test:coverage
|
|
288
|
-
```
|
|
115
|
+
1. **Development Environment**
|
|
116
|
+
```bash
|
|
117
|
+
git clone https://github.com/haowhite/promise-logic.git
|
|
118
|
+
cd promise-logic
|
|
119
|
+
npm install
|
|
120
|
+
```
|
|
121
|
+
2. **Testing**
|
|
122
|
+
```bash
|
|
123
|
+
npm test
|
|
124
|
+
```
|
|
125
|
+
3. **Commit Guidelines**
|
|
126
|
+
- Commit messages must include prefixes like `feat:` (new feature), `fix:` (bug fix), `docs:` (documentation).
|
|
127
|
+
- Pull Requests must include test cases.
|
|
289
128
|
|
|
290
|
-
|
|
129
|
+
---
|
|
291
130
|
|
|
292
|
-
|
|
293
|
-
2. Create a feature branch
|
|
294
|
-
3. Make your changes
|
|
295
|
-
4. Add tests for new functionality
|
|
296
|
-
5. Submit a pull request
|
|
131
|
+
### **8. Resource Links**
|
|
297
132
|
|
|
298
|
-
|
|
133
|
+
- **GitHub Repository**:[https://github.com/xier123456/promise-logic](https://github.com/xier123456/promise-logic)
|
|
134
|
+
- **npm Package**:[https://www.npmjs.com/package/promise-logic](https://www.npmjs.com/package/promise-logic)
|
|
135
|
+
- **Issue Tracking**:[GitHub Issues](https://github.com/xier123456/promise-logic/issues)
|
|
299
136
|
|
|
300
|
-
MIT © 2026
|
package/package.json
CHANGED