From ecdbad329bd8b92733646512106421ce49e54d8d Mon Sep 17 00:00:00 2001 From: Dan Levy <397632+justsml@users.noreply.github.com> Date: Fri, 8 Nov 2019 13:05:29 -0700 Subject: [PATCH] Add retry example to `Tips` readme section (#386) Co-authored-by: Sindre Sorhus --- readme.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/readme.md b/readme.md index 9c826d5e9b..0cfc645f5c 100644 --- a/readme.md +++ b/readme.md @@ -532,6 +532,23 @@ List of [CLI options](https://nodejs.org/api/cli.html#cli_options) passed to the ## Tips +### Retry on error + +Gracefully handle failures by using automatic retries and exponential backoff with the [`p-retry`](https://github.com/sindresorhus/p-retry) package: + +```js +const pRetry = require('p-retry'); + +const run = async () => { + const results = await execa('curl', ['-sSL', 'https://sindresorhus.com/unicorn']); + return results; +}; + +(async () => { + console.log(await pRetry(run, {retries: 5})); +})(); +``` + ### Save and pipe output from a child process Let's say you want to show the output of a child process in real-time while also saving it to a variable.