queue.ls
v0.2.0
Published
Promise based task queue with concurrency limit
Downloads
12
Readme
queue.ls
Promise based task queue with concurrency limit.
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.