promise-tuple 1.1.2 → 1.1.4

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 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
- console.error('Error:', error);
56
+ console.error('Error:', error);
57
57
  } else {
58
- console.log('Data:', data);
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
- id: number;
71
- name: string;
70
+ id: number;
71
+ name: string;
72
72
  }
73
73
 
74
74
  const [error, user] = await promiseTuple<User>(
75
- fetch('/api/user').then(res => res.json())
75
+ fetch('/api/user').then((res) => res.json()),
76
76
  );
77
77
 
78
78
  if (error) {
79
- console.error('Failed to fetch user:', error);
79
+ console.error('Failed to fetch user:', error);
80
80
  } else {
81
- console.log('User:', user?.name);
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
- const [error, response] = await promiseTuple(
90
- fetch(`/api/users/${userId}`)
91
- );
89
+ const [error, response] = await promiseTuple(fetch(`/api/users/${userId}`));
92
90
 
93
- if (error) {
94
- throw new Error(`Failed to fetch user: ${error}`);
95
- }
91
+ if (error) {
92
+ throw new Error(`Failed to fetch user: ${error}`);
93
+ }
96
94
 
97
- const [parseError, user] = await promiseTuple(
98
- response.json()
99
- );
95
+ const [parseError, user] = await promiseTuple(response.json());
100
96
 
101
- if (parseError) {
102
- throw new Error(`Failed to parse user data: ${parseError}`);
103
- }
97
+ if (parseError) {
98
+ throw new Error(`Failed to parse user data: ${parseError}`);
99
+ }
104
100
 
105
- return user;
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
- code: string;
116
- message: string;
111
+ code: string;
112
+ message: string;
117
113
  }
118
114
 
119
115
  const [error, data] = await promiseTuple<string, CustomError>(
120
- someAsyncOperation()
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
- fetch('/api/data'),
133
- () => console.log('Request succeeded!'),
134
- () => console.log('Request failed!')
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
- someAsyncOperation(),
145
- () => {
146
- // This runs on success
147
- console.log('Operation completed successfully');
148
- sendAnalytics('success');
149
- },
150
- () => {
151
- // This runs on failure
152
- console.error('Operation failed');
153
- sendAnalytics('failure');
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
- const [error, user] = await promiseTuple(
194
- db.users.findById(id)
195
- );
189
+ const [error, user] = await promiseTuple(db.users.findById(id));
196
190
 
197
- if (error) {
198
- return { success: false, message: 'User not found' };
199
- }
191
+ if (error) {
192
+ return { success: false, message: 'User not found' };
193
+ }
200
194
 
201
- return { success: true, user };
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
- const [error, content] = await promiseTuple(
213
- fs.readFile(path, 'utf-8')
214
- );
206
+ const [error, content] = await promiseTuple(fs.readFile(path, 'utf-8'));
215
207
 
216
- if (error) {
217
- console.error('Failed to read config:', error);
218
- return null;
219
- }
208
+ if (error) {
209
+ console.error('Failed to read config:', error);
210
+ return null;
211
+ }
220
212
 
221
- return JSON.parse(content);
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
- const [error, user] = await promiseTuple(
233
- db.users.findById(id),
234
- () => {
235
- // Track successful database query
236
- console.log(`User ${id} retrieved successfully`);
237
- metrics.incrementCounter('db.user.success');
238
- },
239
- () => {
240
- // Track failed database query
241
- console.error(`Failed to retrieve user ${id}`);
242
- metrics.incrementCounter('db.user.failure');
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
- return { success: true, user };
242
+ return { success: true, user };
251
243
  }
252
244
  ```
253
245
 
package/dist/index.cjs CHANGED
@@ -25,7 +25,11 @@ __export(index_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
27
  async function promiseTuple(promise, successCallback, failureCallback) {
28
- return promise.then((result) => (successCallback?.(), [void 0, result])).catch((error) => (failureCallback?.(), [error, void 0]));
28
+ return promise.then(
29
+ (result) => (successCallback?.(), [void 0, result])
30
+ ).catch(
31
+ (error) => (failureCallback?.(), [error, void 0])
32
+ );
29
33
  }
30
34
  var index_default = promiseTuple;
31
35
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../index.ts"],"sourcesContent":["/**\r\n * Handles a promise and returns a tuple with error and result.\r\n *\r\n * This function wraps a promise and returns a tuple containing either an error or a result,\r\n * similar to Go's error handling pattern. If the promise resolves, the tuple will contain\r\n * `[undefined, result]`. If the promise rejects, the tuple will contain `[error, undefined]`.\r\n *\r\n * @template R - The type of the resolved value. Defaults to `unknown`.\r\n * @template E - The type of the error. Defaults to `unknown`.\r\n * @param promise - The promise to handle.\r\n * @param successCallback - Optional callback to execute when the promise resolves successfully.\r\n * @param failureCallback - Optional callback to execute when the promise rejects.\r\n * @returns A promise that resolves to a tuple containing either an error or a result.\r\n * On success: `[undefined, result]`\r\n * On failure: `[error, undefined]`\r\n *\r\n * @example\r\n * const [error, data] = await promiseTuple(fetchData());\r\n * if (error) {\r\n * console.error('Error:', error);\r\n * } else {\r\n * console.log('Data:', data);\r\n * }\r\n *\r\n * @example\r\n * const [error, data] = await promiseTuple(\r\n * fetchData(),\r\n * () => console.log('Success!'),\r\n * () => console.log('Failed!')\r\n * );\r\n */\r\nexport async function promiseTuple<R = unknown, E = unknown>(\r\n promise: Promise<R>,\r\n successCallback?: () => void,\r\n failureCallback?: () => void\r\n): Promise<[E | undefined, R | undefined]> {\r\n return promise\r\n .then((result) => (successCallback?.(), [undefined, result] as [undefined, R]))\r\n .catch((error) => (failureCallback?.(), [error, undefined] as [E, undefined]))\r\n}\r\n\r\nexport default promiseTuple\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+BA,eAAsB,aAClB,SACA,iBACA,iBACuC;AACvC,SAAO,QACF,KAAK,CAAC,YAAY,kBAAkB,GAAG,CAAC,QAAW,MAAM,EAAoB,EAC7E,MAAM,CAAC,WAAW,kBAAkB,GAAG,CAAC,OAAO,MAAS,EAAoB;AACrF;AAEA,IAAO,gBAAQ;","names":[]}
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,iBACwC;AACxC,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, R | undefined]>;
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, R | undefined]>;
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 CHANGED
@@ -1,6 +1,10 @@
1
1
  // index.ts
2
2
  async function promiseTuple(promise, successCallback, failureCallback) {
3
- return promise.then((result) => (successCallback?.(), [void 0, result])).catch((error) => (failureCallback?.(), [error, void 0]));
3
+ return promise.then(
4
+ (result) => (successCallback?.(), [void 0, result])
5
+ ).catch(
6
+ (error) => (failureCallback?.(), [error, void 0])
7
+ );
4
8
  }
5
9
  var index_default = promiseTuple;
6
10
  export {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../index.ts"],"sourcesContent":["/**\r\n * Handles a promise and returns a tuple with error and result.\r\n *\r\n * This function wraps a promise and returns a tuple containing either an error or a result,\r\n * similar to Go's error handling pattern. If the promise resolves, the tuple will contain\r\n * `[undefined, result]`. If the promise rejects, the tuple will contain `[error, undefined]`.\r\n *\r\n * @template R - The type of the resolved value. Defaults to `unknown`.\r\n * @template E - The type of the error. Defaults to `unknown`.\r\n * @param promise - The promise to handle.\r\n * @param successCallback - Optional callback to execute when the promise resolves successfully.\r\n * @param failureCallback - Optional callback to execute when the promise rejects.\r\n * @returns A promise that resolves to a tuple containing either an error or a result.\r\n * On success: `[undefined, result]`\r\n * On failure: `[error, undefined]`\r\n *\r\n * @example\r\n * const [error, data] = await promiseTuple(fetchData());\r\n * if (error) {\r\n * console.error('Error:', error);\r\n * } else {\r\n * console.log('Data:', data);\r\n * }\r\n *\r\n * @example\r\n * const [error, data] = await promiseTuple(\r\n * fetchData(),\r\n * () => console.log('Success!'),\r\n * () => console.log('Failed!')\r\n * );\r\n */\r\nexport async function promiseTuple<R = unknown, E = unknown>(\r\n promise: Promise<R>,\r\n successCallback?: () => void,\r\n failureCallback?: () => void\r\n): Promise<[E | undefined, R | undefined]> {\r\n return promise\r\n .then((result) => (successCallback?.(), [undefined, result] as [undefined, R]))\r\n .catch((error) => (failureCallback?.(), [error, undefined] as [E, undefined]))\r\n}\r\n\r\nexport default promiseTuple\r\n"],"mappings":";AA+BA,eAAsB,aAClB,SACA,iBACA,iBACuC;AACvC,SAAO,QACF,KAAK,CAAC,YAAY,kBAAkB,GAAG,CAAC,QAAW,MAAM,EAAoB,EAC7E,MAAM,CAAC,WAAW,kBAAkB,GAAG,CAAC,OAAO,MAAS,EAAoB;AACrF;AAEA,IAAO,gBAAQ;","names":[]}
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,iBACwC;AACxC,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
- "name": "promise-tuple",
3
- "version": "1.1.2",
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
- }
2
+ "name": "promise-tuple",
3
+ "version": "1.1.4",
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
  }