1.0.2 • Published 9 years ago

sync-chain v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

sync-chain

Simple coffee and java script library to do your synchronous tasks synchronously

Why?

No way! Another one sync?

Yes. But this is minimalistic library with no dependencies, written in coffee and easy to use with coffee and vanilla js.

Examples

Note: chain runs functions sync with itself. It isn't blocking.

Including

Just include with

chain = require 'sync-chain'

for coffee, or with

var chain = require('sync-chain');

for javascript.

Fictional example of coffee

chain foo1
    , foo2
    , [fooWithArgs, arg1, arg2]

This is equivalent of following non-chain'ed code:

foo1 (res1) ->
    foo2 res1 (res2) ->
        fooWithArgs res2, arg1, arg2

JS equivalent of fictional example

chain(
    foo1
  , foo2
  , [fooWithArgs, arg1, arg2]
);

This is equivalent of following non-chain'ed code:

foo1(function(res1) {
    foo2(res1, function(res2) {
        fooWithArgs(res2, arg1, arg2);
    }
});

More real example with coffee

So, lets use some real async functions.

Prepairings:

http = require 'http'

collect = (res, next) ->
    data = ''
    res.on 'data', (chunk) -> data += chunk
    res.on 'end', -> next(data)
    
jsonify = (data, next) ->
    setTimeout ->
        next JSON.parse(data)
    , 0

And lets rock with chain:

chain [http.get, 'some url']
    , collect, jsonify, console.log

Testing

Sorry, but i don't provide any tests yet. You may add it yourself.

Contribute

You are welcome to improove this code and write tests.