<script src="js/chain.dev.js" type="text/javascript"></script>
window
, apparently) under the name Chain
. Chain
is the constructor of the new chain and has two main call options (you can think up additional options yourself): var testChain = Chain(); // var anotherChain = new Chain;
.then(Function | Array.<Function>)
.defer(Function | Array.<Function>)
.when(Chain | Array.<Chain>)
.then
method accepts synchronous functions, .defer
- asynchronous, .when
- Chain
objects. All 3 methods return a Chain
object for which they are invoked (just like jQuery chaining ). Also, these methods can take either one required argument or an array of such arguments. For the .then
and .defer
result of the execution of the previous link is passed to the next in the specified function by the first argument..end
method is .end
, it is the last library method. var calculate = Chain(); calculate. then(function() { // undefined return 0; }). then(function(result) { // , result 0 return result + 5; }). then(function(result) { // result 5 return result + 10; }). end(function(result) { console.log(result); }); // 15
var calculate = new Chain; function zero() { return 0; } function plus5(num) { return num + 5; } function plus10(num) { return num + 10; } function log(result) { console.log(result); } calculate. then([zero, plus5, plus10]). end(log); // 15
var calculate = Chain(); calculate. defer(function(n, done) { // undefined, // - , done(0); }). defer(function(result, done) { // result 0 // setTimeout(function() { done(result + 5); }, 1000); }). defer(function(result, done) { // result 5 done(result + 10); }). end(function(result) { console.log(result); }); // 15
var calculate = new Chain; function zero(n, done) { done(0); } function plus5(num, done) { setTimeout(function() { done(num + 5); }, 1000); } function plus10(num, done) { done(num + 10); } function log(result) { console.log(result); } calculate. defer([zero, plus5, plus10]). end(log); // 15
.when
method is used to connect the rest of your chains. After executing the .when
method, the .when
to the next link with the result of executing the previous chain links. Example: var five = Chain(), ten = Chain(); five.defer(function(n, done) { setTimeout(function() { done(5); }, 1000); }); ten. when(five). then(function(results) { return results[0] + 5; }); // results [5] Chain(). when([ten, five, ten]). end(function(results) { console.log(results); }); // [10, 5, 10]
.when
method .when
executed in parallel. The sequence in the resulting array will correspond to the sequence of connection (addition) of the specified chains, see the last example..end
method after the start of the chain will wait for the chain to complete, that is, the chains will not be launched twice. Insert console.log
into each function in the last example, it will become clearer..end
method is .end
; the chain will not be started. In fact, the .end
method can take the second argument, forcing the chain to start again, but this behavior is not brought to mind. If you like Chain.js , then I pledge to deal with this behavior.Source: https://habr.com/ru/post/195588/
All Articles