Change LIFO to FIFO to preserve task order

This commit is contained in:
C-3PO 2018-11-16 03:07:17 +01:00
parent 064e7be78b
commit 758e69ba27
Signed by: c3po
GPG key ID: 62993C4BB4D86F24

View file

@ -1,7 +1,8 @@
export default function taskManager(tasks: Array<() => Promise<void>>, maxConcurrentTasks: number): Promise<any[]> { export default function taskManager(tasks: Array<() => Promise<void>>, maxConcurrentTasks: number): Promise<any[]> {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
const returnValues = Array(tasks.length); const origLength = tasks.length;
const remainingTasks = tasks; const returnValues = Array(origLength);
const remainingTasks = tasks.slice();
let currentlyRunningTasks = 0; let currentlyRunningTasks = 0;
const startNewTask = function() { const startNewTask = function() {
@ -12,8 +13,8 @@ export default function taskManager(tasks: Array<() => Promise<void>>, maxConcur
} }
} else { } else {
//If there is at least one task left, complete it //If there is at least one task left, complete it
const curTask = remainingTasks.pop() as () => Promise<void>; const curTask = remainingTasks.shift() as () => Promise<void>;
const curTaskIndex = remainingTasks.length; const curTaskIndex = origLength - remainingTasks.length;
currentlyRunningTasks += 1; currentlyRunningTasks += 1;
curTask().then(function(...result) { curTask().then(function(...result) {
returnValues[curTaskIndex] = result; returnValues[curTaskIndex] = result;