title: Custom Retry Function
body: <p>Trying to create a function <code>retry</code> that returns a function that calls and returns value from callback function passing its arguments and catches errors. If error is caught it should return the callback function with catch. If number of errors exceeds count then throw an Error.</p>
  <p>Here is what was done so far:</p>
  <pre><code>const retry = (count, callback) =&gt; {
      let attempts = 1;
      const _retry = async (...args) =&gt; callback(...args)
          .catch(err =&gt; {
              if (attempts &gt; count) throw err
              attempts++
              return _retry(...args)
          });
      return _retry
  }
  </code></pre>
  <p>The <a href="https://i.stack.imgur.com/MSbiH.png" rel="nofollow noreferrer">problem</a> appears when called:</p>
  <pre><code>var r = Math.random().toString(36).slice(2)
  var arg = (n) =&gt; async (...v) =&gt;
      --n &lt; 0 ? v : Promise.reject(Error(`&gt;&gt;&gt; x:${v}`))
      
  await retry(3, arg(2))(r)
  </code></pre>