retuple 1.0.0-next.29 → 1.0.0-next.30

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/dist/index.cjs CHANGED
@@ -919,6 +919,43 @@ class ResultAsync {
919
919
  async $unwrap(msg) {
920
920
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrap(msg);
921
921
  }
922
+ /**
923
+ * Resolves this `ResultAsync` using the provided resolver. The resolver
924
+ * receives a promise which resolves when the result is `Ok` and rejects if
925
+ * the result is `Err`.
926
+ *
927
+ * This method should only be called when the `E` type extends `Error`. This
928
+ * is enforced with a type constraint. If the error value is not an instance
929
+ * of Error, the promise rejects with `RetupleExpectFailed`.
930
+ *
931
+ * @example
932
+ *
933
+ * ```ts
934
+ * async function resolver(promise: Promise<URL>): string {
935
+ * try {
936
+ * const url = await promise;
937
+ *
938
+ * return url.toString();
939
+ * } catch (cause) {
940
+ * throw new Error("Not a valid URL", { cause });
941
+ * }
942
+ * }
943
+ *
944
+ * const urlString = await Result.$safeAsync(() => getDataAsync())
945
+ * .$andSafe((data) => new URL(data.url))
946
+ * .$resolve(resolver);
947
+ * ```
948
+ */
949
+ async $resolve(resolver) {
950
+ return __classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (result) => {
951
+ if (result instanceof ResultOk) {
952
+ return await resolver(Promise.resolve(result[1]));
953
+ }
954
+ return await resolver(Promise.reject(result[0] instanceof Error
955
+ ? result[0]
956
+ : new RetupleExpectFailed(result[0])));
957
+ });
958
+ }
922
959
  /**
923
960
  * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
924
961
  * a `Promise`.
package/dist/index.d.cts CHANGED
@@ -212,6 +212,34 @@ declare class ResultAsync<T, E> {
212
212
  * The same as {@link Retuple.$unwrap|$unwrap}, except it returns a `Promise`.
213
213
  */
214
214
  $unwrap(this: ResultAsync<T, E>, msg?: string): Promise<T>;
215
+ /**
216
+ * Resolves this `ResultAsync` using the provided resolver. The resolver
217
+ * receives a promise which resolves when the result is `Ok` and rejects if
218
+ * the result is `Err`.
219
+ *
220
+ * This method should only be called when the `E` type extends `Error`. This
221
+ * is enforced with a type constraint. If the error value is not an instance
222
+ * of Error, the promise rejects with `RetupleExpectFailed`.
223
+ *
224
+ * @example
225
+ *
226
+ * ```ts
227
+ * async function resolver(promise: Promise<URL>): string {
228
+ * try {
229
+ * const url = await promise;
230
+ *
231
+ * return url.toString();
232
+ * } catch (cause) {
233
+ * throw new Error("Not a valid URL", { cause });
234
+ * }
235
+ * }
236
+ *
237
+ * const urlString = await Result.$safeAsync(() => getDataAsync())
238
+ * .$andSafe((data) => new URL(data.url))
239
+ * .$resolve(resolver);
240
+ * ```
241
+ */
242
+ $resolve<U = T>(this: ResultAsync<T, Error>, resolver: (promise: Promise<T>) => PromiseLike<U>): Promise<U>;
215
243
  /**
216
244
  * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
217
245
  * a `Promise`.
package/dist/index.d.ts CHANGED
@@ -212,6 +212,34 @@ declare class ResultAsync<T, E> {
212
212
  * The same as {@link Retuple.$unwrap|$unwrap}, except it returns a `Promise`.
213
213
  */
214
214
  $unwrap(this: ResultAsync<T, E>, msg?: string): Promise<T>;
215
+ /**
216
+ * Resolves this `ResultAsync` using the provided resolver. The resolver
217
+ * receives a promise which resolves when the result is `Ok` and rejects if
218
+ * the result is `Err`.
219
+ *
220
+ * This method should only be called when the `E` type extends `Error`. This
221
+ * is enforced with a type constraint. If the error value is not an instance
222
+ * of Error, the promise rejects with `RetupleExpectFailed`.
223
+ *
224
+ * @example
225
+ *
226
+ * ```ts
227
+ * async function resolver(promise: Promise<URL>): string {
228
+ * try {
229
+ * const url = await promise;
230
+ *
231
+ * return url.toString();
232
+ * } catch (cause) {
233
+ * throw new Error("Not a valid URL", { cause });
234
+ * }
235
+ * }
236
+ *
237
+ * const urlString = await Result.$safeAsync(() => getDataAsync())
238
+ * .$andSafe((data) => new URL(data.url))
239
+ * .$resolve(resolver);
240
+ * ```
241
+ */
242
+ $resolve<U = T>(this: ResultAsync<T, Error>, resolver: (promise: Promise<T>) => PromiseLike<U>): Promise<U>;
215
243
  /**
216
244
  * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
217
245
  * a `Promise`.
package/dist/index.js CHANGED
@@ -905,6 +905,43 @@ class ResultAsync {
905
905
  async $unwrap(msg) {
906
906
  return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$unwrap(msg);
907
907
  }
908
+ /**
909
+ * Resolves this `ResultAsync` using the provided resolver. The resolver
910
+ * receives a promise which resolves when the result is `Ok` and rejects if
911
+ * the result is `Err`.
912
+ *
913
+ * This method should only be called when the `E` type extends `Error`. This
914
+ * is enforced with a type constraint. If the error value is not an instance
915
+ * of Error, the promise rejects with `RetupleExpectFailed`.
916
+ *
917
+ * @example
918
+ *
919
+ * ```ts
920
+ * async function resolver(promise: Promise<URL>): string {
921
+ * try {
922
+ * const url = await promise;
923
+ *
924
+ * return url.toString();
925
+ * } catch (cause) {
926
+ * throw new Error("Not a valid URL", { cause });
927
+ * }
928
+ * }
929
+ *
930
+ * const urlString = await Result.$safeAsync(() => getDataAsync())
931
+ * .$andSafe((data) => new URL(data.url))
932
+ * .$resolve(resolver);
933
+ * ```
934
+ */
935
+ async $resolve(resolver) {
936
+ return __classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (result) => {
937
+ if (result instanceof ResultOk) {
938
+ return await resolver(Promise.resolve(result[1]));
939
+ }
940
+ return await resolver(Promise.reject(result[0] instanceof Error
941
+ ? result[0]
942
+ : new RetupleExpectFailed(result[0])));
943
+ });
944
+ }
908
945
  /**
909
946
  * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
910
947
  * a `Promise`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "retuple",
3
- "version": "1.0.0-next.29",
3
+ "version": "1.0.0-next.30",
4
4
  "scripts": {
5
5
  "test": "vitest",
6
6
  "lint": "eslint . --ext .ts -c eslint.config.mjs --fix",