octopus-async

0.1.2 • Public • Published

Octopus

   /\
 ∠___ゝ  
 )|’ー’| / 
(ノノノ从し' 

Deprecation

Note: this package is deprecated. If you need to do asynchronous tasks you should use promises and/or async/await syntax which should work in all modern browsers as well as in the latest versions of node.js.

You could achieve the same thing as octopus natively:

octopus.run

const results = await Promise.all([one(), two()]);
 
// or
 
const [result1, result2] = await Promise.all([one(), two()]);

octopus.step

const calls = [one, two, three];
let value;
for (const call of calls) {
    value = await call(value);
}

Octopus is a couple of simple asynchronous helper functions. It is designed for use in the browser and is < 500 bytes gzipped. It works in node.js as well.

It is available in NPM

npm install octopus-async

There are two calls

octopus.run

Run a bunch of functions in parallel and receive a callback with the results of all of them.

var calls = [
    function one(callback) {
        setTimeout(function() {
            callback(10);
        }, 200);
    },
    function two(callback) {
        callback(20);
    }
];
 
octopus.run(calls, function(results) {
    console.log(results); // [10, 20]
});

Notice that even though function one finished after function two the results still come back in the order you passed the functions in originally.

You can also pass in an object with names as the keys

var calls = {
    one: function(callback) { callback(10); },
    two: function(callback) { callback(20); }
};
 
octopus.run(calls, function(results){
    console.log(results); // {one: 10, two: 20}
});

octopus.step

Steps through functions one after the other passing the result from one function to the next.

var calls = [
    function one(callback) {
        callback(10);
    },
    function two(value, callback) {
        callback(value * 2);
    },
    function three(value, callback) {
        callback(value * 3);
    }
];
 
octopus.step(calls, function(result) {
    console.log(result); // 60
});

To achieve the same thing with those 3 functions in vanilla Javascript you would have to do something like

one(function(result) {
    two(result, function(result) {
        three(result, function(result) {
            console.log(result);
        });
    });
});

Note about using with node

In node.js the convention is to pass back an error before the actual result in callback functions. To use any of the above examples in node, just be consistent and pass back an error argument as the first argument in your callbacks.

Octopus will stop running and fire the final callback as soon as it receives an error. In the step call it will stop after the step that errored. In the run call it will fire the callback with the error, but the other calls will continue running until they finish. There is no easy way to cancel a function from executing.

Package Sidebar

Install

npm i octopus-async

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

111 kB

Total Files

10

Last publish

Collaborators

  • ccampbell