# ouch-promise-series

> a promise series utility that can access parent value

Latest version **0.1.1** (published 2018-06-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install ouch-promise-series
pnpm add ouch-promise-series
yarn add ouch-promise-series
bun add ouch-promise-series
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2018-06-19 |
| First published | 2018-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 113.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | liqiang372 |
| Maintainers | liqiang372 |

## Links

- npm: https://www.npmjs.com/package/ouch-promise-series
- Repository: https://github.com/liqiang372/ouch-promise-series
- Homepage: https://github.com/liqiang372/ouch-promise-series#readme
- Issues: https://github.com/liqiang372/ouch-promise-series/issues
- npm.io page: https://npm.io/package/ouch-promise-series

## Recent versions

- 0.1.1 (latest) — 2018-06-19
- 0.1.0 — 2018-06-18

## README

Ouch-Promise-Series ![build-badge](https://travis-ci.org/liqiang372/ouch-promise-series.svg?branch=master)
=============
A small promise utility library that allows following promise have access to precedent/parent promises resolved value;

## Installation
```bash
$ npm install ouch-promise-series --save
```

## Basic promises in series
```js
const pSeries = require('ouch-promise-series');

// Real life async calls
function loginUser() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('mary');
    }, 500);
  })
}
function getUserInfo(user) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (user === 'mary') {
        resolve({
          starred: 'romantic'
        });
      } else {
        resolve({})
      }
    }, 300)
  })
}
function getBookListForUser({starred}) {
  return new Promise((resolve, reject) => {
    if (starred === 'romantic') {
      resolve(['book 1', 'book 2', 'book 3']);
    } else {
      resolve(['no book']);
    }
  });
}

pSeries([
  loginUser,
  getUserInfo,
  getBookListForUser
]).then((books) => {console.log(books)}); // ['book 1', 'book 2', 'book 3']
```
By default, promise will receive the resolved value of previous adjacent promise.

## Access early previous promise resolved value
`pSeries` accepts a list where each member could be either of following interface

```js
{
  fn: Function    // required, return a promise
  output?: string // optional, the variable name you give for its resolved value
  input?: string  // optional, the variable you want to take as arguments
}

// or more succinct
fn,            // a normal function returns a promise
[fn, 'output'] // a following normal string will be the output name
[fn, '< input'] // a following string with '<' will be the input
[fn, 'bookList', '< username', '< userPreference'] // they can be combined in whatever order
```

See in action

```js
const createPromise = (time, index) => () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(index);
    }, time)
  })
}

const first = createPromise(800, 1);
const second = createPromise(700, 2);
const third = createPromise(600, 3);
const add = (num1, num2) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(num1 + num2);
    }, 300)
  })
}

pSeries([
  {
    fn: first,
    output: 'firstValue'
  },
  {
    fn: second,
    output: 'secondValue'
  },
  {
    fn: third,
  },
  {
    fn: add,
    input: ['firstValue', 'secondValue']
  }
]).then((result) => {
  console.log(result) // 3
})

// or Array form
pSeries([
  [first, 'firstValue'],
  [second, 'secondValue'],
  third,
  [add, '< firstValue', '<secondValue'] // space is optional
]).then((result) => {
  console.log(result) // 3
});

// or mixed usage

pSeries([
  {
    fn: first,
    output: 'firstValue'
  },
  [second, 'secondValue'],
  [third],
  [add, '< firstValue', '<secondValue'] // space is optional
]).then((result) => {
  console.log(result) // 3
});

```

## LICENSE
MIT

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