@amphibian/promise-retry

promise retries

Usage no npm install needed!

<script type="module">
  import amphibianPromiseRetry from 'https://cdn.skypack.dev/@amphibian/promise-retry';
</script>

README

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