Skip to content

Commit

Permalink
feat: add support for dwait and DeferredPromise.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Dec 31, 2023
1 parent a4cc89e commit 503a332
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ Bring the "Umph" back to the JavaScript error handling!
when(Err, handleError)
);
```
Or even better, You can use the `.dwait` function call on the returned promise of `try$` to get a `DeferredPromise` built using [dwait library](https://github.com/rzvxa/dwait):
```js
const result = await try$(itMayThrow(a, b, c)).dwait().match(
when(Ok, consumeResult),
when(Err, handleError)
).await;
```
With the use of `DeferredPromise` we can keep chaining functions instead of awaiting each result, And we can defer all awaits to the last operation.

Does that seem too rusty? What about something like this? Let's Go!
Does that seem too Rusty to you? What about something like this? Let's Go!
```js
const [res, err] = await try$(itMayThrow(a, b, c));
if (!!err) {
Expand Down Expand Up @@ -55,8 +63,8 @@ axios
.catch((error) => handleError(error));
```

It is the snippet of code from `ErrorHandling` section of `axios` library. So what about `async/await`?
If we want to handle the errors properly with `async/await` pattern we have to write it inside a `try/catch` block like this:
It is the snippet of code from the `ErrorHandling` section of `axios` library. So what about `async/await`?
If we want to handle the errors properly with the `async/await` pattern we have to write it inside a `try/catch` block like this:
```js
try {
const resp = await axios.get("/user/12345");
Expand Down
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "Bring the \"Umph\" to the javascript's async error handling",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": ["dist"],
"files": [
"dist"
],
"scripts": {
"prepare": "ts-node ./scripts/prepare.ts && npm run build",
"lint": "eslint .",
Expand Down Expand Up @@ -56,5 +58,8 @@
"engines": {
"node": "^16.20.2"
},
"engineStrict": true
"engineStrict": true,
"dependencies": {
"dwait": "^1.0.1"
}
}
12 changes: 12 additions & 0 deletions src/Dwaitable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { DeferredPromise } from "dwait";

type Dwaitable<T> = {
/**
* Converts the promise into a `DeferredPromise` from the `dwait` library.
*
* @returns The `DeferredPromise` of the actual result.
*/
dwait: () => DeferredPromise<T>
};

export default Dwaitable;
20 changes: 12 additions & 8 deletions src/try.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import dwait from "dwait";

import type { Result } from "./result";
import type Dwaitable from "./Dwaitable";
import { Ok, Err } from "./result";

/**
Expand All @@ -9,15 +12,16 @@ import { Ok, Err } from "./result";
*
* @returns A `Promise` to a `Result` containing the result of `promise` or the error it may have thrown.
*/
async function try$<TResult, TError>(
function try$<TResult, TError>(
promise: Promise<TResult>
): Promise<Result<TResult, TError>> {
try {
const result: TResult = await promise;
return Ok(result);
} catch (err) {
return Err(err as TError);
}
): Promise<Result<TResult, TError>> & Dwaitable<Result<TResult, TError>> {
const task = Promise.resolve(promise)
.then((result) => Ok<TResult, TError>(result))
.catch((err) => Err(err as TError));
// @ts-expect-error adding dwait to the result promise
task.dwait = () => dwait(task);
return task as Promise<Result<TResult, TError>> &
Dwaitable<Result<TResult, TError>>;
}

export default try$;
5 changes: 5 additions & 0 deletions tests/try.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ describe("try$ Tests", () => {
const result = await try$(resolveMock());
expect(result.isErr()).toEqual(false);
});

test("should return a promise which can be turned into a DeferredPromise using dwait function", async () => {
const result = try$<string, unknown>(resolveMock()).dwait().isErr();
await expect(result).resolves.toEqual(false);
});
});

0 comments on commit 503a332

Please sign in to comment.