# flow.js

> a asynchronous-asynchronous flow control library which runs on node and in browsers

Latest version **0.2.6** (published 2013-02-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install flow.js
pnpm add flow.js
yarn add flow.js
bun add flow.js
```

## 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.2.6 |
| Published | 2013-02-13 |
| First published | 2012-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 62 |
| Author | it-ony |
| Maintainers | it-ony |
| Keywords | flow-control, flow, control, async, asynchronous, chain, sync, synchronous, sequence, parallel, error |

## Links

- npm: https://www.npmjs.com/package/flow.js
- Repository: https://github.com/it-ony/flow.js
- npm.io page: https://npm.io/package/flow.js

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 0.2.6 (latest) — 2013-02-13
- 0.2.5 — 2013-02-13
- 0.2.4 — 2012-05-19
- 0.2.3 — 2012-05-18
- 0.2.2 — 2012-05-16
- 0.2.1 — 2012-05-05
- 0.2.0 — 2012-04-12
- 0.1.1 — 2012-04-05
- 0.1.0 — 2012-03-12

## README

[![build status](https://secure.travis-ci.org/it-ony/flow.js.png)](http://travis-ci.org/it-ony/flow.js)
# flow.js
## Introduction

flow.js is a synchron-asynchron flow control library which runs on node and in browsers.
The functionallity of the library is inspired by node-seq (https://github.com/substack/node-seq) and async (https://github.com/caolan/async). The source is written from scratch.

## Features

* sequence flow (seq)
* parallel flow (par) - executes functions in parallel
* parallel flow for each (parEach) - execute one function in parallel for each item inside a given as array or object
* seqEach - execute sequence for each function given as array
* end  - breaks the flow from inside an action

## Possible features (if somebody like or need)

* catch     - adds a catch action, which will handle erros instead of exec action
* autoexec  - automatically execute flow

## Usage

### Create a flow and execute

```javascript

    flow()
       .exec(function (err, results) {
            if (err) {
                // some error inside the flow
            } else {
                // access to results
            }
       });
```
### Add a sequence

#### execute synchron method in sequence

```javascript
    flow()
       .seq(function(){
           // do something synchron
       })
       .exec(function (err, results) {           
       });

```

#### execute synchron method in sequence

```javascript
    flow()
       .seq(function(cb){
           // do something asynchron
           setTimeout(function(){
                // invoke callback
                cb(null, "result of this sequence");
           }, 1000);
       })
       .exec(function (err, results) {
       });

```


#### sequence with variable registration

```javascript
    flow()
       .seq("myVar", function(){
           return "valueOfMyVar";
       })
       .exec(function (err, results) {
           console.log(results.myVar);
       });

```

### Add parallel control flow

#### asynchron and synchron control parallel control flows

```javascript
    flow()
       .par([
            function() {
                // synchron method
            },
            function (cb) {
                // asynchron method
            }
            // and even more
       ])
       .exec(function (err, results) {
       });

```

#### asynchron and synchron control parallel with variable registration

```javascript
    flow()
       .par({
            a: function() {
                // synchron method
                return 123;
            },
            b: function (cb) {
                // asynchron method
                setTimeout(function(){
                    cb(null, "I completed after 100ms");
                }, 100);
            }
            // and even more
       })
       .exec(function (err, results) {
            console.log(results);
            // results.a = 123;
            // results.b = "I completed after 100ms");
       });

```

#### synchron control parallel each without variable registration

```javascript
    flow()
        .parEach([1, 2, 3], function(value) {
            console.log(value);
        })
        .exec(function (err, results) {
            /*
                output in console could be
                    1      2      3
                    2  or  1  or  2  or ...
                    3      3      1
            */
        })

```

#### synchron control parallel each with variable registration

```javascript
    flow()
        .parEach({
            a: 1,
            b: 2,
            c: 3
        }, function(value) {
            return value*3;
        })
        .exec(function (err, results) {
            console.log(results);   // [3, 6, 9]
        })

```

#### asynchron control parallel each with and without variable registration

```javascript
    flow()
        .parEach(["do", "it"], function(value, cb) {
            setTimeout(function(){
                console.log(value); // synchron
            }, 10);
        })
        .parEach({
            a: 1,
            b: 2
        }, function(value, cb) {
                    setTimeout(function(){
                        cb(null, value*2);// doubles the value and saves it to a or b
                    }, 10);
                })
        .exec(function (err, results) {
            console.log(results);   // {a: 2, b: 4}
        })

```


### end flow from inside an synchron action

```javascript
    flow()
        .seq("a", function() {
            // do something
        }
        .par({
            b: function() {
                // do something synchron in parallel ...

                // end flow on condition
                if (condition) {
                    this.end(); // end flow after return statement
                    return -2;
                }

                return 2;
            },
            c: function(cb) {
                // ... with something asynchron

                // end flow the asynchron way
                this.end(null, 3); // or cb.end(null, 3) or cb(null, 3, true) or this(null, 3, true)
            }
        })
        .seq("e", function() {
            // this would executed, because either b or c will end the flow
        }
        .exec(function(err, results) {
            /* this function is called on
               * error thrown in synchron method
               * error object passed in asynchron method
               * this.end() is called
               * flow is executed completely
            */
        });
```

## Changelog

### 0.2.3

* added global error handler, which get called if an error occurs in exec block

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