async-branch
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "async-branch", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Wrap async library to describe your flow through branches", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -8,4 +8,2 @@ async-branch | ||
See the test folder for more explainations. | ||
Installation | ||
@@ -15,1 +13,67 @@ ------------ | ||
npm install async-branch | ||
Usage | ||
----- | ||
Import the library | ||
var 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('example@example.com', 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. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14186
78