Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allevo/async-branch
Wrap async library to describe your flow through branches
https://github.com/allevo/async-branch
Last synced: 14 days ago
JSON representation
Wrap async library to describe your flow through branches
- Host: GitHub
- URL: https://github.com/allevo/async-branch
- Owner: allevo
- License: mit
- Created: 2014-05-21T20:09:54.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-05-23T23:08:22.000Z (over 10 years ago)
- Last Synced: 2024-04-15T09:13:02.015Z (7 months ago)
- Language: JavaScript
- Size: 168 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
async-branch
============
[![Build Status](https://travis-ci.org/allevo/async-branch.svg?branch=master)](https://travis-ci.org/allevo/async-branch)Wrap async library to describe your flow through branches
Installation
------------npm install async-branch
Usage
-----
Import the libraryvar asyncbranch = require('async-branch')
Create branches and execute
```javascript
var createUser = new asyncbranch.branch('create user')
.do(insertToDb)
.do(sendEmailToConfirmRegistration)
.do(emitUserCreateEvent)
var loginUser = new asyncbranch.branch('load user')
.do(emitUserLoginEvent)var loginOrRegistration = new asyncbranch.branch('login or registration')
.do(loadUserFromEmail)
.branch(function(model, next) {
next(null, model ? loginUser : createUser)
})
.execute('[email protected]', function(err, model) {
// if the user is on db loginUser branch is executed
// else the createUser branch is executed
})
```Or use to map an array
```javascript
var data = [
{ key1: 'pippo' },
{ key1: 'pluto' },
{ key1: 'paperina' },
{ key1: 'paperino' },
]var tooLongFunction = function tooLongFunction(item, next) {
item.type = 'tooLong'
next(null, item)
}
var tooShortFunction = function tooLongFunction(item, next) {
item.type = 'tooShort'
next(null, item)
}var tooLongBranch = new asyncbranch.branch('tooLongBranch')
.map(tooLongFunction)
var tooShortBranch = new asyncbranch.branch('tooShortBranch')
.map(tooShortFunction)function branchFunction(item, callback) {
callback(null, (item.key1.length > 6) ? tooLongBranch : tooShortBranch)
}new asyncbranch.branch('branch name')
.itemBranch(branchFunction)
.execute(data, function(err, data) {
// the first two item of data have type property set to tooShort
// the other items of data have type property set to tooLong
console.log(data)
})
```See the test folder for more explainations.