promise-tuple 1.1.3 → 1.2.0
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 +61 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +71 -61
package/README.md
CHANGED
|
@@ -53,9 +53,9 @@ import promiseTuple from 'promise-tuple';
|
|
|
53
53
|
const [error, data] = await promiseTuple(fetch('/api/data'));
|
|
54
54
|
|
|
55
55
|
if (error) {
|
|
56
|
-
|
|
56
|
+
console.error('Error:', error);
|
|
57
57
|
} else {
|
|
58
|
-
|
|
58
|
+
console.log('Data:', data);
|
|
59
59
|
}
|
|
60
60
|
```
|
|
61
61
|
|
|
@@ -67,18 +67,18 @@ You can specify the type of the resolved value and error:
|
|
|
67
67
|
import promiseTuple from 'promise-tuple';
|
|
68
68
|
|
|
69
69
|
interface User {
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
id: number;
|
|
71
|
+
name: string;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
const [error, user] = await promiseTuple<User>(
|
|
75
|
-
|
|
75
|
+
fetch('/api/user').then((res) => res.json()),
|
|
76
76
|
);
|
|
77
77
|
|
|
78
78
|
if (error) {
|
|
79
|
-
|
|
79
|
+
console.error('Failed to fetch user:', error);
|
|
80
80
|
} else {
|
|
81
|
-
|
|
81
|
+
console.log('User:', user?.name);
|
|
82
82
|
}
|
|
83
83
|
```
|
|
84
84
|
|
|
@@ -86,23 +86,19 @@ if (error) {
|
|
|
86
86
|
|
|
87
87
|
```typescript
|
|
88
88
|
async function getUser(userId: string) {
|
|
89
|
-
|
|
90
|
-
fetch(`/api/users/${userId}`)
|
|
91
|
-
);
|
|
89
|
+
const [error, response] = await promiseTuple(fetch(`/api/users/${userId}`));
|
|
92
90
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
if (error) {
|
|
92
|
+
throw new Error(`Failed to fetch user: ${error}`);
|
|
93
|
+
}
|
|
96
94
|
|
|
97
|
-
|
|
98
|
-
response.json()
|
|
99
|
-
);
|
|
95
|
+
const [parseError, user] = await promiseTuple(response.json());
|
|
100
96
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
97
|
+
if (parseError) {
|
|
98
|
+
throw new Error(`Failed to parse user data: ${parseError}`);
|
|
99
|
+
}
|
|
104
100
|
|
|
105
|
-
|
|
101
|
+
return user;
|
|
106
102
|
}
|
|
107
103
|
```
|
|
108
104
|
|
|
@@ -112,12 +108,12 @@ async function getUser(userId: string) {
|
|
|
112
108
|
import promiseTuple from 'promise-tuple';
|
|
113
109
|
|
|
114
110
|
interface CustomError {
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
code: string;
|
|
112
|
+
message: string;
|
|
117
113
|
}
|
|
118
114
|
|
|
119
115
|
const [error, data] = await promiseTuple<string, CustomError>(
|
|
120
|
-
|
|
116
|
+
someAsyncOperation(),
|
|
121
117
|
);
|
|
122
118
|
```
|
|
123
119
|
|
|
@@ -129,9 +125,9 @@ You can optionally provide success and failure callbacks:
|
|
|
129
125
|
import promiseTuple from 'promise-tuple';
|
|
130
126
|
|
|
131
127
|
const [error, data] = await promiseTuple(
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
128
|
+
fetch('/api/data'),
|
|
129
|
+
() => console.log('Request succeeded!'),
|
|
130
|
+
() => console.log('Request failed!'),
|
|
135
131
|
);
|
|
136
132
|
```
|
|
137
133
|
|
|
@@ -141,17 +137,17 @@ Only the appropriate callback will be executed:
|
|
|
141
137
|
import promiseTuple from 'promise-tuple';
|
|
142
138
|
|
|
143
139
|
const [error, data] = await promiseTuple(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
140
|
+
someAsyncOperation(),
|
|
141
|
+
() => {
|
|
142
|
+
// This runs on success
|
|
143
|
+
console.log('Operation completed successfully');
|
|
144
|
+
sendAnalytics('success');
|
|
145
|
+
},
|
|
146
|
+
() => {
|
|
147
|
+
// This runs on failure
|
|
148
|
+
console.error('Operation failed');
|
|
149
|
+
sendAnalytics('failure');
|
|
150
|
+
},
|
|
155
151
|
);
|
|
156
152
|
```
|
|
157
153
|
|
|
@@ -190,15 +186,13 @@ import promiseTuple from 'promise-tuple';
|
|
|
190
186
|
import db from './database';
|
|
191
187
|
|
|
192
188
|
async function getUserById(id: number) {
|
|
193
|
-
|
|
194
|
-
db.users.findById(id)
|
|
195
|
-
);
|
|
189
|
+
const [error, user] = await promiseTuple(db.users.findById(id));
|
|
196
190
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
191
|
+
if (error) {
|
|
192
|
+
return { success: false, message: 'User not found' };
|
|
193
|
+
}
|
|
200
194
|
|
|
201
|
-
|
|
195
|
+
return { success: true, user };
|
|
202
196
|
}
|
|
203
197
|
```
|
|
204
198
|
|
|
@@ -209,16 +203,14 @@ import promiseTuple from 'promise-tuple';
|
|
|
209
203
|
import fs from 'fs/promises';
|
|
210
204
|
|
|
211
205
|
async function readConfigFile(path: string) {
|
|
212
|
-
|
|
213
|
-
fs.readFile(path, 'utf-8')
|
|
214
|
-
);
|
|
206
|
+
const [error, content] = await promiseTuple(fs.readFile(path, 'utf-8'));
|
|
215
207
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
208
|
+
if (error) {
|
|
209
|
+
console.error('Failed to read config:', error);
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
220
212
|
|
|
221
|
-
|
|
213
|
+
return JSON.parse(content);
|
|
222
214
|
}
|
|
223
215
|
```
|
|
224
216
|
|
|
@@ -229,25 +221,25 @@ import promiseTuple from 'promise-tuple';
|
|
|
229
221
|
import db from './database';
|
|
230
222
|
|
|
231
223
|
async function getUserById(id: number) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
224
|
+
const [error, user] = await promiseTuple(
|
|
225
|
+
db.users.findById(id),
|
|
226
|
+
() => {
|
|
227
|
+
// Track successful database query
|
|
228
|
+
console.log(`User ${id} retrieved successfully`);
|
|
229
|
+
metrics.incrementCounter('db.user.success');
|
|
230
|
+
},
|
|
231
|
+
() => {
|
|
232
|
+
// Track failed database query
|
|
233
|
+
console.error(`Failed to retrieve user ${id}`);
|
|
234
|
+
metrics.incrementCounter('db.user.failure');
|
|
235
|
+
},
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
if (error) {
|
|
239
|
+
return { success: false, message: 'User not found' };
|
|
243
240
|
}
|
|
244
|
-
);
|
|
245
|
-
|
|
246
|
-
if (error) {
|
|
247
|
-
return { success: false, message: 'User not found' };
|
|
248
|
-
}
|
|
249
241
|
|
|
250
|
-
|
|
242
|
+
return { success: true, user };
|
|
251
243
|
}
|
|
252
244
|
```
|
|
253
245
|
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["/**\
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["/**\n * Handles a promise and returns a tuple with error and result.\n *\n * This function wraps a promise and returns a tuple containing either an error or a result,\n * similar to Go's error handling pattern. If the promise resolves, the tuple will contain\n * `[undefined, result]`. If the promise rejects, the tuple will contain `[error, undefined]`.\n *\n * @template R - The type of the resolved value. Defaults to `unknown`.\n * @template E - The type of the error. Defaults to `unknown`.\n * @param promise - The promise to handle.\n * @param successCallback - Optional callback to execute when the promise resolves successfully.\n * @param failureCallback - Optional callback to execute when the promise rejects.\n * @returns A promise that resolves to a tuple containing either an error or a result.\n * On success: `[undefined, result]`\n * On failure: `[error, undefined]`\n *\n * @example\n * const [error, data] = await promiseTuple(fetchData());\n * if (error) {\n * console.error('Error:', error);\n * } else {\n * console.log('Data:', data);\n * }\n *\n * @example\n * const [error, data] = await promiseTuple(\n * fetchData(),\n * () => console.log('Success!'),\n * () => console.log('Failed!')\n * );\n */\nexport async function promiseTuple<R = unknown, E = unknown>(\n promise: Promise<R>,\n successCallback?: () => void,\n failureCallback?: () => void,\n): Promise<[E & {}, undefined] | [undefined, R]> {\n return promise\n .then(\n (result) => (\n successCallback?.(),\n [undefined, result] as [undefined, R]\n ),\n )\n .catch(\n (error) => (\n failureCallback?.(),\n [error, undefined] as [E & {}, undefined]\n ),\n );\n}\n\nexport default promiseTuple;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+BA,eAAsB,aAClB,SACA,iBACA,iBAC6C;AAC7C,SAAO,QACF;AAAA,IACG,CAAC,YACG,kBAAkB,GAClB,CAAC,QAAW,MAAM;AAAA,EAE1B,EACC;AAAA,IACG,CAAC,WACG,kBAAkB,GAClB,CAAC,OAAO,MAAS;AAAA,EAEzB;AACR;AAEA,IAAO,gBAAQ;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
* () => console.log('Failed!')
|
|
30
30
|
* );
|
|
31
31
|
*/
|
|
32
|
-
declare function promiseTuple<R = unknown, E = unknown>(promise: Promise<R>, successCallback?: () => void, failureCallback?: () => void): Promise<[E, undefined] | [undefined, R]>;
|
|
32
|
+
declare function promiseTuple<R = unknown, E = unknown>(promise: Promise<R>, successCallback?: () => void, failureCallback?: () => void): Promise<[E & {}, undefined] | [undefined, R]>;
|
|
33
33
|
|
|
34
34
|
export { promiseTuple as default, promiseTuple };
|
package/dist/index.d.ts
CHANGED
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
* () => console.log('Failed!')
|
|
30
30
|
* );
|
|
31
31
|
*/
|
|
32
|
-
declare function promiseTuple<R = unknown, E = unknown>(promise: Promise<R>, successCallback?: () => void, failureCallback?: () => void): Promise<[E, undefined] | [undefined, R]>;
|
|
32
|
+
declare function promiseTuple<R = unknown, E = unknown>(promise: Promise<R>, successCallback?: () => void, failureCallback?: () => void): Promise<[E & {}, undefined] | [undefined, R]>;
|
|
33
33
|
|
|
34
34
|
export { promiseTuple as default, promiseTuple };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["/**\
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["/**\n * Handles a promise and returns a tuple with error and result.\n *\n * This function wraps a promise and returns a tuple containing either an error or a result,\n * similar to Go's error handling pattern. If the promise resolves, the tuple will contain\n * `[undefined, result]`. If the promise rejects, the tuple will contain `[error, undefined]`.\n *\n * @template R - The type of the resolved value. Defaults to `unknown`.\n * @template E - The type of the error. Defaults to `unknown`.\n * @param promise - The promise to handle.\n * @param successCallback - Optional callback to execute when the promise resolves successfully.\n * @param failureCallback - Optional callback to execute when the promise rejects.\n * @returns A promise that resolves to a tuple containing either an error or a result.\n * On success: `[undefined, result]`\n * On failure: `[error, undefined]`\n *\n * @example\n * const [error, data] = await promiseTuple(fetchData());\n * if (error) {\n * console.error('Error:', error);\n * } else {\n * console.log('Data:', data);\n * }\n *\n * @example\n * const [error, data] = await promiseTuple(\n * fetchData(),\n * () => console.log('Success!'),\n * () => console.log('Failed!')\n * );\n */\nexport async function promiseTuple<R = unknown, E = unknown>(\n promise: Promise<R>,\n successCallback?: () => void,\n failureCallback?: () => void,\n): Promise<[E & {}, undefined] | [undefined, R]> {\n return promise\n .then(\n (result) => (\n successCallback?.(),\n [undefined, result] as [undefined, R]\n ),\n )\n .catch(\n (error) => (\n failureCallback?.(),\n [error, undefined] as [E & {}, undefined]\n ),\n );\n}\n\nexport default promiseTuple;\n"],"mappings":";AA+BA,eAAsB,aAClB,SACA,iBACA,iBAC6C;AAC7C,SAAO,QACF;AAAA,IACG,CAAC,YACG,kBAAkB,GAClB,CAAC,QAAW,MAAM;AAAA,EAE1B,EACC;AAAA,IACG,CAAC,WACG,kBAAkB,GAClB,CAAC,OAAO,MAAS;AAAA,EAEzB;AACR;AAEA,IAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,64 +1,74 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
"name": "promise-tuple",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "A utility function for handling promises with Go-like error handling pattern",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./dist/index.d.cts",
|
|
14
|
+
"default": "./dist/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
25
|
+
"build": "tsup index.ts",
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"lint:fix": "eslint . --fix",
|
|
28
|
+
"format": "prettier . --write",
|
|
29
|
+
"format:check": "prettier . --check",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"prepublishOnly": "npm run clean && npm run build && npm run lint && npm run format:check && npm test"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"promise",
|
|
35
|
+
"error-handling",
|
|
36
|
+
"async",
|
|
37
|
+
"utility",
|
|
38
|
+
"go-style",
|
|
39
|
+
"tuple",
|
|
40
|
+
"await",
|
|
41
|
+
"typescript"
|
|
42
|
+
],
|
|
43
|
+
"author": "Eduardo DONATO <eduardo.donato@gmail.com>",
|
|
44
|
+
"license": "ISC",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/ebdonato/promise-tuple.git"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/ebdonato/promise-tuple/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/ebdonato/promise-tuple#readme",
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=16"
|
|
55
|
+
},
|
|
56
|
+
"files": [
|
|
57
|
+
"dist",
|
|
58
|
+
"README.md",
|
|
59
|
+
"LICENSE"
|
|
60
|
+
],
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@eslint/js": "^10.0.1",
|
|
63
|
+
"@types/node": "^20.0.0",
|
|
64
|
+
"eslint": "^10.0.3",
|
|
65
|
+
"eslint-config-prettier": "^10.1.8",
|
|
66
|
+
"globals": "^17.4.0",
|
|
67
|
+
"prettier": "^3.8.1",
|
|
68
|
+
"tsup": "^8.5.1",
|
|
69
|
+
"tsx": "^4.21.0",
|
|
70
|
+
"typescript": "^5.9.3",
|
|
71
|
+
"typescript-eslint": "^8.57.1",
|
|
72
|
+
"vitest": "^4.0.18"
|
|
20
73
|
}
|
|
21
|
-
},
|
|
22
|
-
"sideEffects": false,
|
|
23
|
-
"scripts": {
|
|
24
|
-
"clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
25
|
-
"build": "tsup index.ts",
|
|
26
|
-
"test": "vitest run",
|
|
27
|
-
"prepublishOnly": "npm run clean && npm run build && npm test"
|
|
28
|
-
},
|
|
29
|
-
"keywords": [
|
|
30
|
-
"promise",
|
|
31
|
-
"error-handling",
|
|
32
|
-
"async",
|
|
33
|
-
"utility",
|
|
34
|
-
"go-style",
|
|
35
|
-
"tuple",
|
|
36
|
-
"await",
|
|
37
|
-
"typescript"
|
|
38
|
-
],
|
|
39
|
-
"author": "Eduardo DONATO <eduardo.donato@gmail.com>",
|
|
40
|
-
"license": "ISC",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "git+https://github.com/ebdonato/promise-tuple.git"
|
|
44
|
-
},
|
|
45
|
-
"bugs": {
|
|
46
|
-
"url": "https://github.com/ebdonato/promise-tuple/issues"
|
|
47
|
-
},
|
|
48
|
-
"homepage": "https://github.com/ebdonato/promise-tuple#readme",
|
|
49
|
-
"engines": {
|
|
50
|
-
"node": ">=16"
|
|
51
|
-
},
|
|
52
|
-
"files": [
|
|
53
|
-
"dist",
|
|
54
|
-
"README.md",
|
|
55
|
-
"LICENSE"
|
|
56
|
-
],
|
|
57
|
-
"devDependencies": {
|
|
58
|
-
"@types/node": "^20.0.0",
|
|
59
|
-
"tsup": "^8.5.1",
|
|
60
|
-
"tsx": "^4.21.0",
|
|
61
|
-
"typescript": "^5.9.3",
|
|
62
|
-
"vitest": "^4.0.18"
|
|
63
|
-
}
|
|
64
74
|
}
|