Pages: 1 2 3 4 5 6

Clumpy Usage Guide

Chains

When a Clumpy method is invoked, it doesn’t immediately begin carrying out its task. Instead, it enqueues the task, to be started when everything before it in queue is finished. So to chain loops together, you can just call the methods one after another:

clumpy.for_loop(
    ...
);
clumpy.while_loop(
    ...
);
clumpy.for_in_loop(
    ...
);

To simplify chaining, every method of a Clumpy instance returns a reference to the instance. Therefore, the code above could be written like this instead:

clumpy.
for_loop(
    ...
).
while_loop(
    ...
).
for_in_loop(
    ...
);

Once

We can chain one-off operations too, not just loops. If we want to perform some action between two loops, we can use the once method. It takes one function as its argument, and performs that function once, with no looping:

var j = 0, key;

clumpy.
while_loop(
    function () { return j < 100000; },
    function () {
        // statements
    }
).
once(function () {
    // statements
}).
for_in_loop(
    function () { return object; },
    function (k) {
        key = k;
        // statements
    }
);

Whatever’s at the end of the chain is effectively the callback:

function countSand (callback) {
    var i = 0;
    
    clumpy.
    while_loop(
        function () { return i < 100000; },
        function () {
            // statements
        }
    ).
    once(callback);
}

Next Page: Indicating Progress

© Thomas Peri