# resolvjs

> Avoid Towers of Terror and Pyramids of Doom

Latest version **1.0.2** (published 2023-11-15) · SEE LICENSE IN license.md license · 0 weekly downloads

## Install

```sh
npm install resolvjs
pnpm add resolvjs
yarn add resolvjs
bun add resolvjs
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-11-15 |
| First published | 2022-10-03 |
| Weekly downloads | 0 |
| License | SEE LICENSE IN license.md |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | GodBleak |
| Maintainers | godbleak |
| Keywords | promise, async, await, readability, Tower of Terror, Pyramid of Doom |

## Links

- npm: https://www.npmjs.com/package/resolvjs
- Repository: https://gitlab.godbleak.dev/pub/resolvjs
- npm.io page: https://npm.io/package/resolvjs

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-11-15
- 1.0.1 — 2022-10-03
- 1.0.0 — 2022-10-03

## README

<div align="center">

# Resolv.js

<hr />

## a small promise handler.

</div>

### Abstract

This is a small utility to make handling promises a little more readable. Avoiding Towers of Terror and Pyramids of Doom.

### Usage:

`resolve()` takes the `promise` to resolve as its first argument, optionally takes a boolean for the second parameter; `convertToError`, finally it optionally takes the parameters of `JSON.stringify` (save for the first) as the remaining parameters, to be used when stringifying errors.

If `convertToError` is

-   `true`, and an error is given that's not an instance of `Error`, `resolve()` will return an instance of `Error` with the given error stringified on the `message` property.
-   given a _falsy_ value, instances of `Object` and `Error` will be returned as-is, while primitives will be treated as if `true` was provided. **This is the default behavior**.
-   explicitly `false`, the error will always be returned as-is. _If you wish for this to be the default behavior, import the named export `resolve` rather than using the default export_.

`resolve()` returns a tuple; `[res, err]`. Where `res` is the result of the promise, and `err` is an error, if one was encountered. The values are mutually exclusive, so if one is defined, the other will be `undefined`.

#### JavaScript:

```js
import resolve from "resolvjs"
import anAsyncFunction from "some-api-client"

const [res, err] = await resolve(anAsyncFunction())
if (err) throw err // or handle it in some other way

console.log(res)
```

#### TypeScript:

`resolve()` is a generic function, enabling you to specify the type of the result in such cases where it cannot be or is incorrectly inferred. However, the returned type for `res` will always be `R | undefined`, this allows you to use a guard pattern, as shown below, to ensure that errors are always handled. After which, `res` will be of type `R`.

```ts
import resolve from "resolvjs"
import anAsyncFunction from "some-api-client"
import type { SuccessResponse } from "some-api-client/types"

const [res, err] = await resolve<SuccessResponse>(anAsyncFunction())
if (err) throw err // or handle it in some other way

console.log(res)
```

#### or:

`resolve()` can also take a type parameter for `err`. When using the default export, this results in `err` being of type `E | Error | undefined`, the example below shows how `err` can be inferred as `E`. When using the named export and no type parameter is provided, `err` will be of type `unknown`. However if a type parameter is provided, it will be of type `E | undefined`.

```ts
import resolve from "resolvjs"
import anAsyncFunction from "some-api-client"
import type { SuccessResponse, ErrorResponse } from "some-api-client/types"

const [res, err] = await resolve<SuccessResponse, ErrorResponse>(anAsyncFunction(), false)
if (err) {
    if ("errorProp" in err) {
        throw err.errorProp
    }
    throw err
}

console.log(res)
```

### Inspiration

Inspiration for this project came from the [Fireship](https://fireship.io/) short [Async Await try-catch hell](https://www.youtube.com/watch?v=ITogH7lJTyE) and uses the solution presented nearly verbatim.

---
_Source: https://npm.io/package/resolvjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
