kidoOooOoooOOom

IT系で開発やってます

asyncの勉強

node.jsでasyncモジュールが結構便利みたい。
series, parallel, waterfallはよく例としても見かけるので理解できた。
今日初めて見たuntilとqueueの2つをメモっておく。

  • async.until

caolan/async · GitHub

until(test, fn, callback)
Repeatedly call fn, until test returns true. Calls the callback when stopped, or an error occurs.
The inverse of async.whilst.

  • async.queue

caolan/async · GitHub

queue(worker, concurrency)

Creates a queue object with the specified concurrency. Tasks added to the queue will be processed in parallel (up to the concurrency limit). If all workers are in progress, the task is queued until one is available. Once a worker has completed a task, the task's callback is called.

Arguments
worker(task, callback) - An asynchronous function for processing a queued task, which must call its callback(err) argument when finished, with an optional error as an argument.
concurrency - An integer for determining how many worker functions should be run in parallel.
Queue objects

The queue object returned by this function has the following properties and methods:
length() - a function returning the number of items waiting to be processed.
concurrency - an integer for determining how many worker functions should be run in parallel. This property can be changed after a queue is created to alter the concurrency on-the-fly.
push(task, [callback]) - add a new task to the queue, the callback is called once the worker has finished processing the task. instead of a single task, an array of tasks can be submitted. the respective callback is used for every task in the list.
unshift(task, [callback]) - add a new task to the front of the queue.
saturated - a callback that is called when the queue length hits the concurrency and further tasks will be queued
empty - a callback that is called when the last item from the queue is given to a worker
drain - a callback that is called when the last item from the queue has returned from the worker