« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/downloaders.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/downloaders.js')
-rw-r--r--src/downloaders.js22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/downloaders.js b/src/downloaders.js
index fa1f337..2b193eb 100644
--- a/src/downloaders.js
+++ b/src/downloaders.js
@@ -1,3 +1,5 @@
+'use strict'
+
 const fs = require('fs')
 const fetch = require('node-fetch')
 const promisifyProcess = require('./promisify-process')
@@ -47,8 +49,26 @@ function makeLocalDownloader() {
   }
 }
 
+function makePowerfulDownloader(downloader, maxAttempts = 5) {
+  // This should totally be named better..
+
+  return async function recursive(arg, attempts = 0) {
+    try {
+      return await downloader(arg)
+    } catch(err) {
+      if (attempts < maxAttempts) {
+        console.warn('Failed - attempting again:', arg)
+        return await recursive(arg, attempts + 1)
+      } else {
+        throw err
+      }
+    }
+  }
+}
+
 module.exports = {
   makeHTTPDownloader,
   makeYouTubeDownloader,
-  makeLocalDownloader
+  makeLocalDownloader,
+  makePowerfulDownloader
 }