« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/common-util/sugar.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/common-util/sugar.js')
-rw-r--r--src/common-util/sugar.js22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js
index aeb0fe4f..c7e15dd3 100644
--- a/src/common-util/sugar.js
+++ b/src/common-util/sugar.js
@@ -360,13 +360,27 @@ export function queue(functionList, queueSize = 50) {
       thisReject(error);
     } finally {
       running--;
-      next();
+
+      // If the cursor is at 1, this is the first promise that resolved,
+      // so we're now done the "kick start", and can start the remaining
+      // promises (up to queueSize).
+      if (cursor === 1) {
+        // Since only one promise is used for the "kick start", and that one
+        // has just resolved, we know there's none running at all right now,
+        // and can start as many as specified in the queueSize right away.
+        for (let i = 0; i < queueSize; i++) {
+          setTimeout(next);
+        }
+      } else {
+        setTimeout(next);
+      }
     }
   };
 
-  for (let i = 0; i < queueSize; i++) {
-    next();
-  }
+  // Only start a single promise, as a "kick start", so that it resolves as
+  // early as possible (it will resolve before we use CPU to start the rest
+  // of the promises, up to queueSize).
+  next();
 
   return promiseList;
 }