Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Asynchronous function queue with adjustable concurrency.

This module exports a class `Queue` that implements most of the `Array` API. Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods. Processing begins when you call `q.start()`.

## Example
## Basic Usage Example
Do `npm run example` or `npm run dev` and open the example directory (and your console) to run the following program:
``` javascript
import Queue from 'queue'
Expand Down Expand Up @@ -109,6 +109,52 @@ q.start(err => {
})
```

## Finding and Removing a Specific Job Example

```javascript
q = new Queue({
concurrency: 1,
timeout: null,
autostart: false,
});

// Fill the queue with jobs.
['job1', 'job2', 'job3', 'job4'].forEach((jobTitle) => {
// Build the job using a promise.
const job = () => {
return new Promise((resolve) => {
// Automatically resolve the job after 10 seconds.
setTimeout(resolve, 10000);
});
};

// Add a custom property to the job so we can search for it later.
job.jobTitle = jobTitle;

// Add the job to the queue.
q.push(job);
});

let jobIndex = null;

// We want to remove job2.
// Go through the list of jobs to find which has the "jobTitle" property we're looking for.
Object.entries(q.jobs).some(([index, job]) => {
if (job.jobTitle === 'job2') {
jobIndex = index;

return true;
}

return false;
});

// Remove this job from the queue if we found its index.
if (jobIndex !== null) {
q.jobs.splice(jobIndex, 1);
}
```

## Install

```
Expand Down Expand Up @@ -169,6 +215,15 @@ Ensures the queue is always running if jobs are available. Useful in situations
### `q.results`
An array to set job callback arguments on.

### `q.pending`
The number of jobs currently running.

### `q.running`
Boolean indicating whether or not the queue is currently running.

### `q.jobs`
An array of job functions queued to be ran. This does not include the currently running job.

### `q.length`
Jobs pending + jobs to process (readonly).

Expand Down