0.2.0 • Published 9 years ago

queue.ls v0.2.0

Weekly downloads
1
License
Unlicense
Repository
-
Last release
9 years ago

queue.ls

Promise based task queue with concurrency limit. NPM version dep

Example

const https = require('https')
const url = require('url')
const queue = require('queue.ls')

const addTask = queue(3)
for (let i = 1; i <= 20; i++) {
  addTask(fetchPage(i)).then(result => console.log(result))
}

function fetchPage(i) {
  const options = url.parse(
    `https://api.github.com/repos/jquery/jquery/commits?page=${i}`)
  options.headers = { 'User-Agent': 'Awesome-Octocat-App' }
  return () => new Promise(resolve =>
    https.get(options, res => {
      const buffer = []
      res.on('data', chunk => buffer.push(chunk))
      res.on('end', () => resolve(JSON.parse(buffer.join(''))))
    }))
};

Example

require! https
require! url
queue = require \queue.ls

add-task = queue 3
fetch-page = (i) ->
  options = url.parse \
  "https://api.github.com/repos/jquery/jquery/commits?page=#i"
  options <<< headers: 'User-Agent': \Awesome-Octocat-App
  -> new Promise (resolve) ->
    res <- https.get options
    buffer = []
    res.on \data buffer~push
    <- res.on \end
    resolve JSON.parse buffer.join ''

for i from 1 to 20
  add-task fetch-page i .then -> console.log it

Install

npm i --save queue.ls

queue(concurrency=1)

  • concurrency: Maximum number of tasks should run concurrently.

Returns a function to add a task.

add(task)

  • task <Function>

The function returned by queue().

Returns a promise, resolves to the result of task.

Starts the task immediately if concurrency limit is not reached, enqueues it otherwise. Queued tasks start after some previous tasks end, in the order they added.

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago