Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krasimir/chain
Sequencing function calls in JavaScript
https://github.com/krasimir/chain
Last synced: 3 days ago
JSON representation
Sequencing function calls in JavaScript
- Host: GitHub
- URL: https://github.com/krasimir/chain
- Owner: krasimir
- Created: 2013-07-08T13:05:53.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-07-10T13:54:13.000Z (over 11 years ago)
- Last Synced: 2024-04-11T07:12:24.821Z (7 months ago)
- Language: JavaScript
- Size: 109 KB
- Stars: 18
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Chain.js
=====Sequencing function calls in JavaScript.
## Examples
### #1 (simple)
Chain()(
function(res, chain) {
console.log("A"); chain.next(10);
},
function(res, chain) {
console.log("B", res); chain.next(res+1);
},
function(res, chain) {
console.log("C", res); chain.next();
}
);Output:
A
B 10
C 11### #2 (passing function as array)
var A = function(str, callback) {
console.log("Hello " + str + "!");
callback();
}
var B = function(str1, str2, callback) {
console.log("How " + str1 + " " + str2 + "?");
callback();
}
Chain()(
[A, "world"],
[B, "are", "you"],
function(res, chain) {
chain.next();
}
);Output:
Hello world!
How are you?### #3 (#1 + #2 + listening for chain ending)
var chainFinished = function(res, chain) {
console.log("End", res);
}
var customFunc = function(arg1, arg2, callback) {
console.log("customFunc", arg1, arg2);
callback("result(customFunc)");
}Chain()("done", chainFinished)(
function(res, chain) {
console.log("A", res);
chain.next("result(A)");
},
[customFunc, "Hello", "World"],
function(res, chain) {
console.log("B", res);
chain.next("result(B)");
}
);Output:
A null
customFunc Hello World
B result(customFunc)
End result(B)### #4 (error reporting)
var operationA = function(res, chain) {
chain.error({message: "Error message from operation A"}).next();
}
var operationB = function(res, chain) {
chain.error({message: "Error message from operation B"}).next();
}
Chain()("done", function(res, chain) {
if(errors = chain.error()) {
console.log("Yes, there are some errors", errors);
}
})(
operationA,
operationB
);Output:
Yes, there are some errors
[Object, Object]