# Installation

Use the npm command in order to install the promises-chain module:

```bash
npm install [--save] promises-chain
```

# Chaining promise and avoid ugly code

The promises-chain tools provide a function to chain promises together. It avoids ugly code like this:

```js
toto.then(function() {
  tata.then(function() {
    titi.then(function {
      ...
    });
  });
});
```

  - exec(promises) : chain the array of "promises" together. Return a promise which is resolved when the chain is over and is rejected when one node is broken.

  Promises format:

  ```json
  {
    "scope" : object or null
    "func" : string or func
    "arg" : any or null
  }
  ```

  scope: if object, the func will be called from scope
  func: use string if scope is not null, otherwise use func
  arg: if any force thar argument of the promise to arg, otherwise taking as argument the result of the previous promise


# Usage exemple

```js
var q = require('q');
var promisesChain = require('promises-chain');

var toto = function(number) {
  var deferred = q.defer();

  console.log(number);
  deferred.resolve(number + 1);

  return deferred.promise;
};

// Display 1, 2, 3
promisesChain.exec([
  {
    'scope' : null,
    'func' : toto,
    'args' : 1
  },
  {
    'scope' : null,
    'func' : toto,
    'args' : null
  },
  {
    'scope' : null,
    'func' : toto,
    'args' : null
  }
]);

```
