diff --git a/lib/internal/DoublyLinkedList.js b/lib/internal/DoublyLinkedList.js index 4444bcb94..39f88ac6c 100644 --- a/lib/internal/DoublyLinkedList.js +++ b/lib/internal/DoublyLinkedList.js @@ -60,14 +60,16 @@ export default class DLL { return this.tail && this.removeLink(this.tail); } - toArray () { - var arr = Array(this.length); - var curr = this.head; - for(var idx = 0; idx < this.length; idx++) { - arr[idx] = curr.data; - curr = curr.next; + toArray() { + return [...this] + } + + *[Symbol.iterator] () { + var cur = this.head + while (cur) { + yield cur.data + cur = cur.next } - return arr; } remove (testFn) { diff --git a/lib/internal/queue.js b/lib/internal/queue.js index a811080eb..4c1f5cc5f 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -87,6 +87,9 @@ export default function queue(worker, concurrency, payload) { var isProcessing = false; var q = { _tasks: new DLL(), + *[Symbol.iterator] () { + yield* q._tasks[Symbol.iterator]() + }, concurrency, payload, saturated: noop, diff --git a/lib/queue.js b/lib/queue.js index 00b90cee9..0fa7b9685 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -3,7 +3,7 @@ import wrapAsync from './internal/wrapAsync'; /** * A queue of tasks for the worker function to complete. - * @typedef {Object} QueueObject + * @typedef {Iterable} QueueObject * @memberOf module:ControlFlow * @property {Function} length - a function returning the number of items * waiting to be processed. Invoke with `queue.length()`. @@ -53,6 +53,18 @@ import wrapAsync from './internal/wrapAsync'; * @property {Function} kill - a function that removes the `drain` callback and * empties remaining tasks from the queue forcing it to go idle. No more tasks * should be pushed to the queue after calling this function. Invoke with `queue.kill()`. + * + * @example + * const q = aync.queue(worker, 2) + * q.push(item1) + * q.push(item2) + * q.push(item3) + * // queues are iterable, spread into an array to inspect + * const items = [...q] // [item1, item2, item3] + * // or use for of + * for (let item of q) { + * console.log(item) + * } */ /** diff --git a/test/queue.js b/test/queue.js index 3eeeac8c0..2ac50778d 100644 --- a/test/queue.js +++ b/test/queue.js @@ -799,4 +799,23 @@ describe('queue', function(){ done(); } }); + + it('should be iterable', (done) => { + var q = async.queue((data, cb) => { + if (data === 3) { + q.push(6) + expect([...q]).to.eql([4, 5, 6]); + } + async.setImmediate(cb); + }); + + q.push([1, 2, 3, 4, 5]); + + expect([...q]).to.eql([1, 2, 3, 4, 5]); + + q.drain = function () { + expect([...q]).to.eql([]); + done(); + } + }) });