# @eweilow/stale-while-revalidate

> A very simple stale-while-revalidate module with built-in batching

Latest version **0.0.1-alpha.1** (published 2020-02-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @eweilow/stale-while-revalidate
pnpm add @eweilow/stale-while-revalidate
yarn add @eweilow/stale-while-revalidate
bun add @eweilow/stale-while-revalidate
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1-alpha.1 |
| Published | 2020-02-05 |
| First published | 2020-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | eweilow |
| Maintainers | eweilow |
| Keywords | stale-while-revalidate |

## Links

- npm: https://www.npmjs.com/package/@eweilow/stale-while-revalidate
- Repository: https://github.com/eweilow/stale-while-revalidate
- Issues: https://github.com/eweilow/stale-while-revalidate/issues
- npm.io page: https://npm.io/package/@eweilow/stale-while-revalidate

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.0.1-alpha.1 (latest) — 2020-02-05
- 0.0.1-alpha.0 — 2020-02-02

## README

# stale-while-revalidate

A very simple stale-while-revalidate module with built-in batching

## Usage

The simplest usage is the following:

```typescript
async function fetchData() {
  // fetch data
  const value = await fn(); // ..whatever

  return {
    value,
    maxAgeSeconds: 1 // Set to whatever you want
  };
}

const loader = singleStaleWhileRevalidate(fetchData);

async function loadData() {
  const { stale, value } = await loader();
  // Value is now fetched if it has not been fetched before
  // Otherwise the last fetched value is returned
  // While a new fetch is initiated if it is stale
}
```

There is a special usage of the `loader`-function, which uses the optional first argument:

```typescript
const loader = singleStaleWhileRevalidate(fetchData);

async function loadData() {
  const fresh = true;
  const { stale, value } = await loader(fresh);
  // We now never receive a stale response.
  // invariant: if fresh == true, then stale == false
}
```

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