promise-logic 2.4.5 → 2.4.7
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 +41 -19
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -6,18 +6,20 @@
|
|
|
6
6
|
**Replace API Memory with Logical Concepts**
|
|
7
7
|
The design philosophy of `promise-logic` is: **Developers should focus on business logic, not the details of Promise APIs**.
|
|
8
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 `
|
|
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.
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
13
|
### **2. Features**
|
|
14
14
|
|
|
15
15
|
1. **Logical Semantics**
|
|
16
|
-
- `
|
|
17
|
-
- `
|
|
18
|
-
- `
|
|
19
|
-
- `
|
|
20
|
-
|
|
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
|
+
|
|
21
|
+
- `not`: Inverts the result of a single Promise
|
|
22
|
+
- `majority`: Most tasks succeed
|
|
21
23
|
|
|
22
24
|
2. **Zero Dependencies**
|
|
23
25
|
Only depends on native Promise, no additional runtime dependencies.
|
|
@@ -43,7 +45,7 @@ npm install promise-logic
|
|
|
43
45
|
|
|
44
46
|
#### Example: Primary/Backup Service Call (XOR Scenario)
|
|
45
47
|
```javascript
|
|
46
|
-
import {
|
|
48
|
+
import { PromiseLogic } from 'promise-logic';
|
|
47
49
|
|
|
48
50
|
// Primary service call
|
|
49
51
|
const primary = fetch('https://api.main.com/data');
|
|
@@ -51,7 +53,7 @@ const primary = fetch('https://api.main.com/data');
|
|
|
51
53
|
const backup = fetch('https://api.backup.com/data');
|
|
52
54
|
|
|
53
55
|
// Execute XOR logic: exactly one success
|
|
54
|
-
|
|
56
|
+
PromiseLogic.xor([primary, backup])
|
|
55
57
|
.then(result => {
|
|
56
58
|
console.log('Successfully fetched data:', result);
|
|
57
59
|
})
|
|
@@ -66,7 +68,25 @@ XOR(primary, backup)
|
|
|
66
68
|
|
|
67
69
|
#### Example: Majority Decision (Majority Scenario)
|
|
68
70
|
```javascript
|
|
69
|
-
import {
|
|
71
|
+
import { PromiseLogic } from 'promise-logic';
|
|
72
|
+
|
|
73
|
+
const services = [
|
|
74
|
+
fetch('https://api.node1.com/vote'),
|
|
75
|
+
fetch('https://api.node2.com/vote'),
|
|
76
|
+
fetch('https://api.node3.com/vote')
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
PromiseLogic.majority(services)
|
|
80
|
+
.then(results => {
|
|
81
|
+
console.log('Majority of services returned success:', results);
|
|
82
|
+
})
|
|
83
|
+
.catch(error => {
|
|
84
|
+
console.error('Majority of services failed:', error);
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { PromiseLogic } from 'promise-logic/typescript';
|
|
70
90
|
|
|
71
91
|
const services = [
|
|
72
92
|
fetch('https://api.node1.com/vote'),
|
|
@@ -74,7 +94,7 @@ const services = [
|
|
|
74
94
|
fetch('https://api.node3.com/vote')
|
|
75
95
|
];
|
|
76
96
|
|
|
77
|
-
|
|
97
|
+
PromiseLogic.majority<Response>(services)
|
|
78
98
|
.then(results => {
|
|
79
99
|
console.log('Majority of services returned success:', results);
|
|
80
100
|
})
|
|
@@ -89,24 +109,26 @@ Majority(...services)
|
|
|
89
109
|
|
|
90
110
|
| API | Description |
|
|
91
111
|
| :--------- | :-------------------------------------------------------------------------- |
|
|
92
|
-
| `
|
|
93
|
-
| `
|
|
94
|
-
| `
|
|
95
|
-
| `
|
|
96
|
-
| `
|
|
112
|
+
| `and` | All Promises succeed, returns result array; any failure causes overall failure. |
|
|
113
|
+
| `or` | At least one Promise succeeds, returns first success result; all failures cause overall failure. |
|
|
114
|
+
| `xor` | **Exactly one Promise succeeds**, returns that result; otherwise throws `XOR_ERROR`. |
|
|
115
|
+
| `nand` | All Promises fail, returns error array; any success causes overall failure. |
|
|
116
|
+
| `not` | Inverts the result of a single Promise |
|
|
117
|
+
| `majority` | More than half of Promises succeed, returns success result array; otherwise overall failure. |
|
|
97
118
|
|
|
98
119
|
---
|
|
99
120
|
|
|
100
121
|
### **6. Real-world Application Scenarios**
|
|
101
122
|
|
|
102
123
|
1. **Primary/Backup Service Calls**
|
|
103
|
-
- Use `
|
|
124
|
+
- Use `xor` to ensure **exactly one service responds**, avoiding duplicate processing.
|
|
104
125
|
2. **Distributed Decision Making**
|
|
105
|
-
- Use `
|
|
126
|
+
- Use `majority` to implement majority consensus (e.g., distributed voting).
|
|
106
127
|
3. **Resource Competition**
|
|
107
|
-
- Use `
|
|
128
|
+
- Use `or` to get the first available resource (e.g., CDN node selection).
|
|
129
|
+
- Use `not` to check if a resource is available.
|
|
108
130
|
4. **Full-link Validation**
|
|
109
|
-
- Use `
|
|
131
|
+
- Use `and` to ensure all dependent services succeed (e.g., order creation).
|
|
110
132
|
|
|
111
133
|
---
|
|
112
134
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "promise-logic",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.7",
|
|
4
4
|
"description": "Compose promises with logic gate semantics (AND, OR, XOR, NAND, NOR, XNOR, Majority). Forget APIs, remember logic.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs.js",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./dist/index.esm.js",
|
|
12
|
-
"require": "./dist/
|
|
13
|
-
"types": "./dist/types/
|
|
12
|
+
"require": "./dist/index.cjs.js",
|
|
13
|
+
"types": "./dist/types/index.d.ts"
|
|
14
14
|
},
|
|
15
15
|
"./factory": {
|
|
16
16
|
"import": "./dist/factory.esm.js",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"types": "./dist/types/factory.d.ts"
|
|
19
19
|
},
|
|
20
20
|
"./typescript": {
|
|
21
|
-
"import": "./dist/index.esm.js",
|
|
22
|
-
"require": "./dist/index.cjs.js",
|
|
23
|
-
"types": "./dist/types/
|
|
21
|
+
"import": "./dist/v2/index.esm.js",
|
|
22
|
+
"require": "./dist/v2/index.cjs.js",
|
|
23
|
+
"types": "./dist/v2/types/index.d.ts"
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"files": [
|