# @expo/results

> An efficient, standards-compliant library for representing results of successful or failed operations

Latest version **1.0.0** (published 2020-04-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @expo/results
pnpm add @expo/results
yarn add @expo/results
bun add @expo/results
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-04-29 |
| First published | 2019-12-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 16.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 106 |
| Author | Expo |
| Maintainers | bbarthec, brentvatne, ccheever, charliecruzan, dsokal, esamelson, evanbacon, expoadmin, exponent, fson, ide, ijzerenhein, jkhales, mczernek, quinlanj, sjchmiela, tcdavis, tsapeta, wkozyra, wschurman |
| Keywords | result, fulfilled, rejected, allsettled |

## Links

- npm: https://www.npmjs.com/package/@expo/results
- Repository: https://github.com/expo/results
- Homepage: https://github.com/expo/results#readme
- Issues: https://github.com/expo/results/issues
- npm.io page: https://npm.io/package/@expo/results

## Recent versions

- 1.0.0 (latest) — 2020-04-29
- 0.3.0 — 2020-01-23
- 0.2.0 — 2020-01-08
- 0.1.1 — 2019-12-30
- 0.1.0 — 2019-12-21

## README

# @expo/results
[![Tests](https://github.com/expo/results/workflows/Tests/badge.svg)](https://github.com/expo/results/actions?query=branch%3Amaster)
[![codecov](https://codecov.io/gh/expo/results/branch/master/graph/badge.svg)](https://codecov.io/gh/expo/results)

An efficient, standards-compliant library for representing results of successful or failed operations. A result object represents the result of an operation that can either return a value successfully or fail. Typically we'd simply either return a value or throw an error, but sometimes we perform multiple operations as a batch, some of which may succeed and others fail. Since we can't simultaneously return values and throw errors, we instead return collections of result objects. This allows a batch operation to return values for successful operations and errors for failed ones without loss of information, namely the errors. (In contrast, sometimes it is appropriate for a batch operation to return just successful values and omit values for failed operations.)

# Usage

## Using Results

```ts
import { Result, result } from '@expo/results';

const results = await fetchWebPages(['https://expo.dev', 'http://example.com']);
for (const result of results) {
  if (result.ok) {
    console.log(result.value);
  } else {
    console.error(result.reason);
  }
}
```

## Creating Results

```ts
import { Result, result } from '@expo/results';

/**
 * The purpose of this result API is to let you write functions that can
 * partially succeed and partially fail and return all of that information to
 * the caller.
 */
function fetchWebPages(urls: string[]): Promise<Result<string>[]> {
  return Promise.all(urls.map(fetchWebPage));
}

function fetchWebPage(url: string): Promise<Result<string>> {
  try {
    const response = await fetch(url);
    const text = await response.text();
    return result(text);
  } catch (e) {
    return result(e);
  }
}
```

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