@open-draft/until
Advanced tools
+21
| MIT License | ||
| Copyright (c) 2021 Artem Zakharchenko | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
+2
-1
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.until = void 0; | ||
| var until_1 = require("./until"); | ||
| exports.until = until_1.until; | ||
| Object.defineProperty(exports, "until", { enumerable: true, get: function () { return until_1.until; } }); |
+9
-2
@@ -0,6 +1,13 @@ | ||
| export declare type AsyncTuple<ErrorType extends any = Error, DataType extends any = unknown> = { | ||
| error: ErrorType; | ||
| data: null; | ||
| } | { | ||
| error: null; | ||
| data: DataType; | ||
| }; | ||
| /** | ||
| * Gracefully handles a given Promise factory. | ||
| * @example | ||
| * cosnt [error, data] = await until(() => asyncAction()) | ||
| * const [error, data] = await until(() => asyncAction()) | ||
| */ | ||
| export declare const until: <DataType = unknown, ErrorType = Error>(promise: () => Promise<DataType>) => Promise<[ErrorType, DataType]>; | ||
| export declare const until: <ErrorType extends unknown = Error, DataType extends unknown = unknown>(promise: () => Promise<DataType>) => Promise<AsyncTuple<ErrorType, DataType>>; |
+6
-4
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.until = void 0; | ||
| /** | ||
| * Gracefully handles a given Promise factory. | ||
| * @example | ||
| * cosnt [error, data] = await until(() => asyncAction()) | ||
| * const [error, data] = await until(() => asyncAction()) | ||
| */ | ||
| exports.until = async (promise) => { | ||
| const until = async (promise) => { | ||
| try { | ||
@@ -13,7 +14,8 @@ const data = await promise().catch((error) => { | ||
| }); | ||
| return [null, data]; | ||
| return { error: null, data }; | ||
| } | ||
| catch (error) { | ||
| return [error, null]; | ||
| return { error, data: null }; | ||
| } | ||
| }; | ||
| exports.until = until; |
+6
-5
| { | ||
| "name": "@open-draft/until", | ||
| "version": "1.0.3", | ||
| "version": "2.0.0", | ||
| "description": "Gracefully handle a Promise using async/await.", | ||
@@ -14,8 +14,9 @@ "main": "lib/index.js", | ||
| "devDependencies": { | ||
| "@types/jest": "^25.1.4", | ||
| "jest": "^25.1.0", | ||
| "ts-jest": "^25.2.1", | ||
| "typescript": "^3.8.3" | ||
| "@types/jest": "^26.0.22", | ||
| "jest": "^26.6.3", | ||
| "ts-jest": "^26.5.4", | ||
| "typescript": "^4.2.4" | ||
| }, | ||
| "scripts": { | ||
| "start": "tsc -w", | ||
| "test": "jest", | ||
@@ -22,0 +23,0 @@ "build": "tsc --build tsconfig.json", |
+8
-5
@@ -0,1 +1,2 @@ | ||
| [](https://www.npmjs.com/package/@open-draft/until) | ||
| [](https://circleci.com/gh/open-draft/until) | ||
@@ -44,2 +45,4 @@ | ||
| npm install @open-draft/until | ||
| # or | ||
| yarn add @open-draft/until | ||
| ``` | ||
@@ -53,3 +56,3 @@ | ||
| async function(id) { | ||
| const [error, user] = await until(() => fetchUser(id)) | ||
| const { error, data } = await until(() => fetchUser(id)) | ||
@@ -60,3 +63,3 @@ if (error) { | ||
| return user | ||
| return data | ||
| } | ||
@@ -81,3 +84,3 @@ ``` | ||
| async function(id: string) { | ||
| const [error, user] = await until<User, UserFetchError>(() => fetchUser(id)) | ||
| const { error, data } = await until<UserFetchError, User>(() => fetchUser(id)) | ||
@@ -88,3 +91,3 @@ if (error) { | ||
| return user.firstName | ||
| return data.firstName | ||
| } | ||
@@ -95,2 +98,2 @@ ``` | ||
| - [giuseppegurgone](https://twitter.com/giuseppegurgone) for discussing the original `until` API | ||
| - [giuseppegurgone](https://twitter.com/giuseppegurgone) for the discussion about the original `until` API. |
5273
45.66%7
16.67%39
34.48%94
3.3%