# deference

> Utilities for jQuery's Deferred object.

Latest version **2.0.0** (published 2013-09-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install deference
pnpm add deference
yarn add deference
bun add deference
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2013-09-30 |
| First published | 2013-04-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Nathan Bryan |
| Maintainers | nbryan |
| Keywords | jQuery, Deferred |

## Links

- npm: https://www.npmjs.com/package/deference
- Repository: https://github.com/nbryan/deference
- Issues: https://github.com/nbryan/deference/issues
- npm.io page: https://npm.io/package/deference

## Dependencies (1)

- [jquery](https://npm.io/package/jquery.md) >= 1.5

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2013-09-30
- 1.1.0 — 2013-08-01
- 1.0.1 — 2013-05-09
- 1.0.0 — 2013-04-29

## README

# Deference

Deference is a set of three utility methods for use with
[jQuery's Deferred ojbect](http://api.jquery.com/category/deferred-object/). They're similar to
jQuery's built in `$.when()` method in that they simply compose more complex deferred patterns into
simple and reusable methods.

## Installation

Require jQuery like you normall would, then include Deference.

```html
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="deference.js"></script>
```

## Usage

Deference simply adds three functions to the jQuery object: parallel, serial, and wait.

Parallel and serial are similar to `$.each()` in that they iterate through an array passing each
item to a function. The main difference is that the function must return a deferred object (or a
promise). When all the inner deferreds are resolved, then the outer deferred is resolved
(or rejected). After each iteration the outer deferred is also notified with the number of
iterations completed.

### Parallel

As you might expect, parallel executes all the function calls at once in parallel. This is useful
when many tasks must be completed before moving on.

```javascript
var items = [1, 2, 3, 4];

$.parallel(items, function(i) {
  return $.wait(500); // Do something more interesting here
}).progress(function(completed, total, percentage) {
  console.log(percentage + '%'); // 25%, 50%, 75%, and 100% will be logged simultaneously
}).done(function() {
  console.log('done!')
});
```

### Serial

Again, as you might expect, serial waits for each iteration to complete before starting the next.
This is useful if each iteration is dependent on one the previous or if the order of execution is
important.

```javascript
var items = [1, 2, 3, 4];

$.serial(items, function(i) {
  return $.wait(500); // Do something more interesting here
}).progress(function(completed, total, percentage) {
  console.log(percentage + '%'); // 25%, 50%, 75%, and 100% will be logged one at a time
}).done(function() {
  console.log('done!');
});
```

### Wait

Wait waits for a specified period of time before resolving. The default is 1000ms. If you'd like to
execute some logic after another deferred finishes _and_ after a specified period of time, use wait.

Sound pretty useless? Imagine a case where you need to post many things to Facebook at a time
without getting rate-limited. Using serial in combination with wait would prevent Facebook from
rate-limiting your posts.

```javascript
var items = [1, 2, 3, 4];

$.serial(items, function(i) {
  return $.when(postToFacebook(i), $.wait(500));
})
```

## Failure modes

### Partial Failure / Success

Deference supports treating partial failures as a success. `$.serial`
and `$.parallel` both take an options hash, with a valid option being
`threshhold`. This is the highest allowed number of failures, that
will still consider the overall operation a success. For example, if
at least half of the following calls succeed, the enclosing deferred
will also succeed:

```javascript
var items = [1, 2, 3, 4];

$.serial(items, function(i) {
  return $.when(doSomethingInteresting(i));
}, { threshhold: items.length / 2 });
```

## About

Deference was written and is maintained by [Nathan Bryan](https://github.com/nbryan). It is freely
available under the MIT license. If you find it useful, let me know! Or submit a pull request if
you can improve it.

### Contributors

- [Alejandro Ciniglio](https://github.com/ciniglio)

---
_Source: https://npm.io/package/deference · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
