@open-draft/until
Advanced tools
Comparing version 1.0.3 to 2.0.0
"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; } }); |
@@ -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>>; |
"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; |
{ | ||
"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", |
@@ -0,1 +1,2 @@ | ||
[![Latest release](https://img.shields.io/npm/v/@open-draft/until.svg)](https://www.npmjs.com/package/@open-draft/until) | ||
[![Build status](https://circleci.com/gh/open-draft/until.svg?style=shield)](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. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5273
7
39
94