This question is a daily question and will count towards your daily streak.

You're building a task queue that processes items in batches. What will be the final state of the processed array?

Async programmingPromisesArrays+1

index.js

1class TaskQueue {
2 constructor(batchSize = 2) {
3 this.batchSize = batchSize;
4 this.processed = [];
5 }
6
7 async processTasks(tasks) {
8 for (let i = 0; i < tasks.length; i += this.batchSize) {
9 const batch = tasks.slice(i, i + this.batchSize);
10 const results = await Promise.all(
11 batch.map(task => this.processTask(task))
12 );
13 this.processed.push(...results);
14 }
15 return this.processed;
16 }
17
18 async processTask(task) {
19 return `${task}-done`;
20 }
21}
22
23const queue = new TaskQueue(2);
24const tasks = ['A', 'B', 'C'];
25
26queue.processTasks(tasks).then(console.log);
Stats

Total submissions:

0