1.2.0 • Published 2 years ago

@amphibian/promise-retry v1.2.0

Weekly downloads
7
License
MIT
Repository
github
Last release
2 years ago

@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.

1.2.0

2 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago