Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kgo

Package Overview
Dependencies
Maintainers
2
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kgo

Flow control the super easy way

  • 3.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.5K
decreased by-6.85%
Maintainers
2
Weekly downloads
 
Created
Source

kgo

Stupidly easy flow control.

Why

flow contol should be seamless, you should be able to say what you want done, and say kgo.

Usage

kgo(result name, [dependencies], asynchronous function); // -> self

where result name is an arbitrary string that represents the result of the function,

dependencies is an array of strings that map to the result of another function,

and asynchronous function is a function that, when complete, calls a callback with it's results.

kgo returns its-self, so it can be chained:

kgo
(name, deps, fn)
(name, deps, fn)
(name, deps, fn);

Example

require kgo:

var kgo = require('./kgo');

use kgo:

kgo
('things', function(done){

    //Something async
    setTimeout(function(){
        done(null, 1);
    }, 100);

})
('stuff', function(done){

    //Something async
    setTimeout(function(){
        done(null, 2);
    }, 100);

})
('whatsits', ['things', 'stuff'], function(things, stuff, done){

    //Something async
    setTimeout(function(){
        done(null, things + stuff);
    }, 100);

})
('dooby', ['things'], function(things, done){

    //Something async
    setTimeout(function(){
        done(null, things/2);
    }, 100);

})
(['whatsits', 'dooby'], function(whatsits, dooby, done){

    //Done
    console.log(whatsits, dooby);

});

The above will log 3, 0.5;

Async Mapping

Removed as of version 2. Use foreign instead.

Ignoring dependency results

You will often not need the result of a dependency, and it's annoying to have unused parameters in your functions. You can specify that you have a dependancy, whos result you don't want, by prefixing the dependancy name with an exclamation mark:

kgo

('a', function(done){
    done(null, 'foo');
})

('b', ['!a'], function(done){
    done(null, 'bar');
})

(['b'],  function(b){
    // here b will be "bar"
});

Defaults

You can define default data for use in later tasks by passing an object into kgo, where the keys in the objects will map to dependency names:

kgo

({
    foo: 1
})

('bar', function(done){
    done(null, 2);
})

('baz', ['foo', 'bar'], function(foo, bar, done){

});

This is especially useful when you want to use named functions that need additional parameters to run:

var fs = require('fs');

kgo
({
    'sourcePath': '/foo/bar'
})
('files', ['sourcePath'], fs.readdir);

Note: You may only define defaults once in a kgo block. Extra calls will result in an error.

Multiple results

You can return more than one result in a single task by giving your task multiple names, and returning more results in the callback

kgo

('foo', 'bar', function(done){
    done(null, 2, 4);
})

('baz', ['foo', 'bar'], function(foo, bar, done){
    // foo === 2
    // bar === 4
});

Errors

You can handler errors from specific tasknames by prefixing the taskname with an astrix (*)

kgo
('task1', task1)
('task2', task2)
(['*task1'], function(tast1error){
    // Called if task1 errored
})
(['*task2'], function(tast2error){
    // Called if task2 errored
})
(['task1', 'task2'], function(result1, result2){
    // Called if task1 and task2 succeded.
});

there is an implicit taskName, *error, that will resolve any task that depends on it, even if it has other unresolved dependencies.

kgo
(task)
(another task)
(['*error'], function(error){
    // Called if any error occurs in any task
});

You can combine this with other dependencies to build a callback task:

function doThings(callback){

    kgo
    ('task1', ...)
    ('task2', ...)
    ('result', ['task1', 'task2'], ...)
    (['*error', 'result'], callback); // Will be called either if any task errors, OR if result resolves.

}

FAQs

Package last updated on 15 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc