✨ Change LIFO to FIFO to preserve task order
This commit is contained in:
parent
064e7be78b
commit
758e69ba27
1 changed files with 5 additions and 4 deletions
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue