Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@open-draft/until

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@open-draft/until - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

lib/index.js.map

16

lib/index.d.ts

@@ -1,1 +0,15 @@

export { until } from './until';
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
* const { error, data } = await until(() => asyncAction())
*/
declare const until: <ErrorType extends unknown = Error, DataType extends unknown = unknown>(promise: () => Promise<DataType>) => Promise<AsyncTuple<ErrorType, DataType>>;
export { until };

@@ -1,5 +0,41 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.until = void 0;
var until_1 = require("./until");
Object.defineProperty(exports, "until", { enumerable: true, get: function () { return until_1.until; } });
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
until: () => until
});
module.exports = __toCommonJS(src_exports);
// src/until.ts
var until = async (promise) => {
try {
const data = await promise().catch((error) => {
throw error;
});
return { error: null, data };
} catch (error) {
return { error, data: null };
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
until
});
//# sourceMappingURL=index.js.map

22

package.json
{
"name": "@open-draft/until",
"version": "2.0.0",
"version": "2.1.0",
"description": "Gracefully handle a Promise using async/await.",
"main": "lib/index.js",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"require": "./lib/index.js",
"import": "./lib/index.mjs",
"default": "./lib/index.mjs"
}
},
"repository": "open-draft/until",

@@ -14,5 +24,7 @@ "author": "Artem Zakharchenko <kettanaito@gmail.com>",

"devDependencies": {
"@ossjs/release": "^0.5.1",
"@types/jest": "^26.0.22",
"jest": "^26.6.3",
"ts-jest": "^26.5.4",
"tsup": "^6.2.3",
"typescript": "^4.2.4"

@@ -23,5 +35,5 @@ },

"test": "jest",
"build": "tsc --build tsconfig.json",
"prepublishOnly": "yarn test && yarn build"
"build": "tsup",
"release": "release publish"
}
}
}
[![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)

@@ -45,4 +44,2 @@ # `until`

npm install @open-draft/until
# or
yarn add @open-draft/until
```

@@ -92,4 +89,64 @@

## Frequently asked questions
### Why does `until` accept a function and not a `Promise` directly?
This has been intentionally introduced to await a single logical unit as opposed to a single `Promise`.
```js
// Notice how a single "until" invocation can handle
// a rather complex piece of logic. This way any rejections
// or exceptions happening within the given function
// can be handled via the same "error".
const { error, data } = until(async () => {
const user = await fetchUser()
const nextUser = normalizeUser(user)
const transaction = await saveModel('user', user)
invariant(transaction.status === 'OK', 'Saving user failed')
return transaction.result
})
if (error) {
// Handle any exceptions happened within the function.
}
```
### Why does `until` return an object and not an array?
The `until` function used to return an array of shape `[error, data]` prior to `2.0.0`. That has been changed, however, to get proper type-safety using discriminated union type.
Compare these two examples:
```ts
const [error, data] = await until(() => action())
if (error) {
return null
}
// Data still has ambiguous "DataType | null" type here
// even after you've checked and handled the "error" above.
console.log(data)
```
```ts
const result = await until(() => action())
// At this point, "data" is ambiguous "DataType | null"
// which is correct, as you haven't checked nor handled the "error".
if (result.error) {
return null
}
// Data is strict "DataType" since you've handled the "error" above.
console.log(result.data)
```
> It's crucial to keep the entire result of the `Promise` in a single variable and not destructure it. TypeScript will always keep the type of `error` and `data` as it was upon destructuring, ignoring any type guards you may perform later on.
## Special thanks
- [giuseppegurgone](https://twitter.com/giuseppegurgone) for the discussion about the original `until` API.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc