Comparing version 1.1.2 to 1.2.0
import type { Result } from "./result"; | ||
import type Dwaitable from "./Dwaitable"; | ||
/** | ||
@@ -10,4 +11,4 @@ * It will try and catch the possible errors during execution of the given `Promise`, | ||
*/ | ||
declare function try$<TResult, TError>(promise: Promise<TResult>): Promise<Result<TResult, TError>>; | ||
declare function try$<TResult, TError>(promise: Promise<TResult>): Promise<Result<TResult, TError>> & Dwaitable<Result<TResult, TError>>; | ||
export default try$; | ||
//# sourceMappingURL=try.d.ts.map |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const dwait_1 = __importDefault(require("dwait")); | ||
const result_1 = require("./result"); | ||
@@ -12,12 +16,11 @@ /** | ||
*/ | ||
async function try$(promise) { | ||
try { | ||
const result = await promise; | ||
return (0, result_1.Ok)(result); | ||
} | ||
catch (err) { | ||
return (0, result_1.Err)(err); | ||
} | ||
function try$(promise) { | ||
const task = Promise.resolve(promise) | ||
.then((result) => (0, result_1.Ok)(result)) | ||
.catch((err) => (0, result_1.Err)(err)); | ||
// @ts-expect-error adding dwait to the result promise | ||
task.dwait = () => (0, dwait_1.default)(task); | ||
return task; | ||
} | ||
exports.default = try$; | ||
//# sourceMappingURL=try.js.map |
{ | ||
"name": "tryumph", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Bring the \"Umph\" to the javascript's async error handling", | ||
@@ -61,3 +61,6 @@ "main": "./dist/index.js", | ||
}, | ||
"engineStrict": true | ||
"engineStrict": true, | ||
"dependencies": { | ||
"dwait": "^1.0.1" | ||
} | ||
} |
@@ -18,4 +18,12 @@ # tryumph | ||
``` | ||
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 | ||
@@ -59,4 +67,4 @@ const [res, err] = await try$(itMayThrow(a, b, c)); | ||
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 | ||
@@ -63,0 +71,0 @@ try { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39207
35
662
151
1
+ Addeddwait@^1.0.1
+ Addeddwait@1.1.4(transitive)