\begin{quote}
Retry a promise-\/returning or async function \end{quote}
It does exponential backoff and supports custom retry strategies for failed operations.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19320}{}\doxysection{Install}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19320}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install p-\/retry }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19321}{}\doxysection{Usage}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19321}

\begin{DoxyCode}{0}
\DoxyCodeLine{const pRetry = require('p-\/retry');}
\DoxyCodeLine{const fetch = require('node-\/fetch');}
\DoxyCodeLine{}
\DoxyCodeLine{const run = async () => \{}
\DoxyCodeLine{    const response = await fetch('https://sindresorhus.com/unicorn');}
\DoxyCodeLine{}
\DoxyCodeLine{    // Abort retrying if the resource doesn't exist}
\DoxyCodeLine{    if (response.status === 404) \{}
\DoxyCodeLine{        throw new pRetry.AbortError(response.statusText);}
\DoxyCodeLine{    \}}
\DoxyCodeLine{}
\DoxyCodeLine{    return response.blob();}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{(async () => \{}
\DoxyCodeLine{    console.log(await pRetry(run, \{retries: 5\}));}
\DoxyCodeLine{\})(); }

\end{DoxyCode}


With the {\ttfamily on\+Failed\+Attempt} option\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const run = async () => \{}
\DoxyCodeLine{    const response = await fetch('https://sindresorhus.com/unicorn');}
\DoxyCodeLine{}
\DoxyCodeLine{    if (response.status !== 200) \{}
\DoxyCodeLine{        throw new Error(response.statusText);}
\DoxyCodeLine{    \}}
\DoxyCodeLine{}
\DoxyCodeLine{    return response.json();}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{(async () => \{}
\DoxyCodeLine{    const result = await pRetry(run, \{}
\DoxyCodeLine{        onFailedAttempt: error => \{}
\DoxyCodeLine{            console.log(`Attempt \$\{error.attemptNumber\} failed. There are \$\{error.retriesLeft\} retries left.`);}
\DoxyCodeLine{            // 1st request => Attempt 1 failed. There are 4 retries left.}
\DoxyCodeLine{            // 2nd request => Attempt 2 failed. There are 3 retries left.}
\DoxyCodeLine{            // …}
\DoxyCodeLine{        \},}
\DoxyCodeLine{        retries: 5}
\DoxyCodeLine{    \});}
\DoxyCodeLine{}
\DoxyCodeLine{    console.log(result);}
\DoxyCodeLine{\})(); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19322}{}\doxysection{API}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19322}
\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19323}{}\doxysubsection{p\+Retry(input, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19323}
Returns a {\ttfamily Promise} that is fulfilled when calling {\ttfamily input} returns a fulfilled promise. If calling {\ttfamily input} returns a rejected promise, {\ttfamily input} is called again until the max retries are reached, it then rejects with the last rejection reason.

It doesn\textquotesingle{}t retry on {\ttfamily Type\+Error} as that\textquotesingle{}s a user error.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19324}{}\doxysubsubsection{input}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19324}
Type\+: {\ttfamily Function}

Receives the number of attempts as the first argument and is expected to return a {\ttfamily Promise} or any value.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19325}{}\doxysubsubsection{options}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19325}
Type\+: {\ttfamily Object}

Options are passed to the \href{https://github.com/tim-kos/node-retry\#retryoperationoptions}{\texttt{ {\ttfamily retry}}} module.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19326}{}\doxyparagraph{on\+Failed\+Attempt(error)}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19326}
Type\+: {\ttfamily Function}

Callback invoked on each retry. Receives the error thrown by {\ttfamily input} as the first argument with properties {\ttfamily attempt\+Number} and {\ttfamily retries\+Left} which indicate the current attempt number and the number of attempts left, respectively.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19327}{}\doxysubsection{p\+Retry.\+Abort\+Error(message$\vert$error)}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19327}
Abort retrying and reject the promise.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19328}{}\doxysubsection{message}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19328}
Type\+: {\ttfamily string}

Error message.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19329}{}\doxysubsection{error}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19329}
Type\+: {\ttfamily Error}

Custom error.\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19330}{}\doxysection{Tip}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19330}
You can pass arguments to the function being retried by wrapping it in an inline arrow function\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const pRetry = require('p-\/retry');}
\DoxyCodeLine{}
\DoxyCodeLine{const run = async emoji => \{}
\DoxyCodeLine{    // …}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{(async () => \{}
\DoxyCodeLine{    // Without arguments}
\DoxyCodeLine{    await pRetry(run, \{retries: 5\});}
\DoxyCodeLine{}
\DoxyCodeLine{    // With arguments}
\DoxyCodeLine{    await pRetry(() => run('🦄'), \{retries: 5\});}
\DoxyCodeLine{\})(); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19331}{}\doxysection{Related}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19331}

\begin{DoxyItemize}
\item \href{https://github.com/sindresorhus/p-timeout}{\texttt{ p-\/timeout}} -\/ Timeout a promise after a specified amount of time
\item \href{https://github.com/sindresorhus/promise-fun}{\texttt{ More…}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_p_retry_readme_autotoc_md19332}{}\doxysection{License}\label{md_sfcode_node_modules_p_retry_readme_autotoc_md19332}
MIT © \href{https://sindresorhus.com}{\texttt{ Sindre Sorhus}} 