Skip to content

Instantly share code, notes, and snippets.

@dk00

dk00/LICENSE

Last active Oct 10, 2016
Embed
What would you like to do?
queue.ls - promise based task queue with concurrency limit

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.

This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
{
"name": "queue.ls",
"version": "0.2.0",
"description": "Promise based task queue with concurrency limit",
"keywords": [
"queue",
"concurrency",
"promise"
],
"author": "dk00 <dk00@users.noreply.github.com>",
"homepage": "https://gist.github.com/dk00/024f6d09b12c58c5ff91a4625350fd58",
"bugs": "https://gist.github.com/dk00/024f6d09b12c58c5ff91a4625350fd58",
"license": "Unlicense",
"files": [
"queue.js"
],
"main": "queue.js",
"scripts": {
"prepublish": "lsc -bc queue",
"test": "lsc test.ls"
},
"repository": "gist:024f6d09b12c58c5ff91a4625350fd58",
"devDependencies": {
"livescript": ""
}
}
module.exports = queue <<< {queue} if module?
function queue(concurrency=1)
running = 0
waiting = []
(task) ->
result = if running < concurrency
running++
task!
else
new Promise (resolve) -> waiting.push -> !resolve task!
result
..then -> running-- unless waiting.shift!?!
require! assert
queue = require \./queue
expected = 3
add-task = queue expected
running = 0
max-conrurrent = 0
Promise.all [to 5]map (i) ->
add-task ->
console.log "run #i"
running++
max-conrurrent >?= running
new Promise (resolve) ->
<- (-> setTimeout it, 200)
running--
console.log "done #i"
resolve!
.then ->
assert max-conrurrent == expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment