# fuuu

> TypeScript utility to safely execute functions

Latest version **0.0.8** (published 2024-08-09) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2024-08-09 |
| First published | 2024-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | oktaysenkan |
| Keywords | typescript, async, promise, error-handling, utility |

## Links

- npm: https://www.npmjs.com/package/fuuu
- Homepage: https://github.com/oktaysenkan/fuuu
- npm.io page: https://npm.io/package/fuuu

## 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

- 0.0.8 (latest) — 2024-08-09
- 0.0.7 — 2024-08-09
- 0.0.6 — 2024-08-09
- 0.0.5 — 2024-08-09
- 0.0.4 — 2024-08-09
- 0.0.3 — 2024-08-08
- 0.0.2 — 2024-08-08
- 0.0.1 — 2024-08-07

## README

<p align="center">
  <img src="https://github.com/user-attachments/assets/d2796ecb-9aa4-4e42-a172-1d4d1d9a96fd" width="150px" />
</p>
<h1 align="center">fuu</h1>
<p align="center">
Don’t lose your mind, safely execute functions.
</p>

<hr />

## Installation

You can install this package using npm:

```sh
npm install fuuu
```

## Usage

```ts
import * as f from "fuuu"

interface People {
  name: string
  height: string
  mass: string
  gender: string
}

const getVaderDetails = async () => {
  const response = await fetch("https://swapi.dev/api/people/4/")
  return response.json() as Promise<People>
}

const main = async () => {
  const vader = await f.safe(getVaderDetails)

  if (vader.error) {
    // error handling
    return
  }

  console.log(vader.data)
}
```

#### Retries

```ts
const vader = await f.safe(getVaderDetails, {
  retries: 3,
  retryDelay: 1000,
})
```

#### Timeout

```ts
const vader = await f.safe(getVaderDetails, {
  timeout: 3000,
})

const isTimeoutError = vader.error instanceof f.TimeoutError
```

## Motivation

In software development, handling asynchronous operations and potential errors gracefully is crucial for building robust applications. Traditionally, developers rely on try-catch blocks to manage errors in asynchronous code. While this approach works, it can lead to verbose and repetitive code, making it harder to maintain and reason about.

The fuuu library simplifies this process by providing a clean and concise way to handle errors in asynchronous functions. Instead of wrapping every function call in a try-catch block, fuuu allows you to safely execute functions and handle errors more elegantly. With features like retries and timeouts, fuuu ensures that your application can handle transient errors and network issues without losing stability.

By using fuuu, you reduce boilerplate code and make your error-handling strategy more consistent across your application. This not only improves code readability but also makes it easier to manage complex asynchronous workflows, leading to more reliable and maintainable software.

```ts
import * as f from "fuuu"

const withTryCatch = async () => {
  let profile: Profile | null = null

  try {
    profile = await fetchUserProfileById(1)
  } catch (e) {
    throw new Error("An error occurred while fetching profile")
  }

  try {
    const friends = await fetchFriendsByAccountId(profile.accountId)

    return { profile, friends }
  } catch (e) {
    return { profile, friends: null }
  }
}

const withFuuu = async () => {
  const profile = await f.safe(() => fetchUserProfileById(1))

  if (profile.error) throw new Error("An error occurred while fetching profile")

  const friends = await f.safe(() =>
    fetchFriendsByAccountId(profile.data.accountId),
  )

  return { profile: profile.data, friends: friends.data }
}
```

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