@amphibian/promise-retry

1.2.0 • Public • Published

@amphibian/promise-retry

promise retries

npm install @amphibian/promise-retry

Usage

import retry from '@amphibian/promise-retry';

async function getUsers() {
	const response = await retry(() => fetch('https://reqres.in/api/users'));
	const user = await response.json();

	return user;
}

With options

retry(() => (
	fetch('https://reqres.in/api/users').then((response) => response.json())
), {attempts: 3, timeout: 250});

Intentionally aborting retries

const fetchUsers = () => (
	fetch('https://reqres.in/api/users')
		.then((response) => {
			if (response.status === 404) {
				// This will prevent subsequent retries
				throw new retry.AbortError('resource_not_found');

				// You can also clone an existing error
				const error = new Error('my custom error');
				error.code = 'my custom property';

				// Properties from `error` are cloned
				throw new retry.AbortError(error);
			}

			return response.json();
		})
);

retry(fetchUsers, {attempts: 3})
	.catch((error) => {
		console.log(error.name); // > AbortError
	});

retry(function, options)

function (Function)

The function to retry. Should return a Promise or be async.

options (Object)

options.attempts (Number)

Default: 3 Number of times to retry before throwing the error from the final attempt.

options.timeout (Number)

Default: 250 Number of milliseconds to wait between retries.

Readme

Keywords

none

Package Sidebar

Install

npm i @amphibian/promise-retry

Weekly Downloads

4

Version

1.2.0

License

MIT

Unpacked Size

11.8 kB

Total Files

7

Last publish

Collaborators

  • thomaslindstr_m