# @h2ml/fetchdelay

> Wrapper function for fetch() which allows for better control over timing.

Latest version **1.0.4** (published 2023-02-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @h2ml/fetchdelay
pnpm add @h2ml/fetchdelay
yarn add @h2ml/fetchdelay
bun add @h2ml/fetchdelay
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2023-02-19 |
| First published | 2023-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jack Notman |
| Maintainers | h2ml |
| Keywords | fetch, timeout, promise |

## Links

- npm: https://www.npmjs.com/package/@h2ml/fetchdelay
- Repository: https://github.com/jack-h2ml/fetchDelay
- Homepage: https://github.com/jack-h2ml/fetchDelay#readme
- Issues: https://github.com/jack-h2ml/fetchDelay/issues
- npm.io page: https://npm.io/package/@h2ml/fetchdelay

## Dependencies (2)

- [core-js](https://npm.io/package/core-js.md) ^3.28.0
- [@h2ml/bettertimeout](https://npm.io/package/@h2ml/bettertimeout.md) ^1.0.3

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2023-02-19
- 1.0.3 — 2023-02-19
- 1.0.2 — 2023-02-19
- 1.0.1 — 2023-02-19
- 1.0.0 — 2023-02-19

## README

## About

The @h2ml/fetchdelay library provides a simple function which wraps the native <code>fetch()</code> method, allowing for control over when the returned <code>Promise</code> resolves or rejects. 

## Quick Start Guide

### The [fetchDelay](#module_@h2ml/fetchreplace.fetchDelay) Function

The <code>fetchDelay</code> function is invoked by providing a <code>url</code> and <code>options</code>, this will return a <code>Promise</code>, which will not resolve until after the duration specified by <code>options.minDelay</code> and will reject if the internal <code>fetch()</code> call does not resolve before the duration specified by <code>options.maxDelay</code>.

```
import { fetchDelay } from '@h2ml/fetchdelay';

(async () => {
	try {
		// This will resolve after a minimum of 3 seconds, 
		// and will reject after 5 seconds if the resource does not load.
		const res1 = await fetchDelay('https://ipapi.co/json', {
			minDelay: 3000,
			maxDelay: 5000
		});
		const json1 = await res1.json();
		console.log(json1);

		// This will reject, as the requested resource 
		// will not resolve until after 6 seconds.
		const res2 = await fetchDelay('https://httpbin.org/delay/6', {
			minDelay: 3000,
			maxDelay: 5000
		});
		const json2 = await res2.json(); // This won't get called.
		console.log(json)2;              // Neither will this.
	} catch (err) {
		// Catch-all rejections.
		console.dir(err);
	}
})();
```

<a name="module_@h2ml/fetchreplace"></a>

## @h2ml/fetchreplace
**Version**: 1.0.3  
**Author**: Jack Notman  
<a name="module_@h2ml/fetchreplace.fetchDelay"></a>

### @h2ml/fetchreplace.fetchDelay ⇒ <code>Promise.&lt;Response&gt;</code> \| <code>Error</code>
The [fetchDelay](fetchDelay) function.

**Kind**: static constant of [<code>@h2ml/fetchreplace</code>](#module_@h2ml/fetchreplace)  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| url | <code>String</code> |  | The url to fetch. |
| options | <code>Object</code> |  | fetchDelay Configuration options. |
| [options.minDelay] | <code>Number</code> | <code>1000</code> | The minimum delay before the Promise resolves. |
| [options.maxDelay] | <code>Number</code> | <code>3000</code> | The maximum amount of time before the Promise rejects. |
| [options.fetchOptions] | <code>Object</code> | <code>{}</code> | The configuration options to pass to fetch(). |

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