Code Monkey home page Code Monkey logo

asynx's Introduction

Async extensions

Adds several utilities on top of async.js. Aimed to be used as drop-in replacement:

var async = require('asynx');
// or be more explicit
var asynx = require('asynx');

Installation

npm install asynx

Usage

asynx.return(results..., callback)

Hijack callback into returning predefined result(s). Error is propagated unchanged.

asynx.waterfall([
    // get response and body of an url
    asynx.apply(request.get, url),
    // write body to file, but return http response from waterfall
    function (response, body, callback) {
        fs.writefile(filename, asynx.return(response, callback))
    }
], callback)

asynx.shift

asynx.waterfall([
    // get response and body of an url
    asynx.apply(request.get, url),
    // throw away response and pass body to fs.writeFile
    asynx.shift,
    // write body to a file
    asynx.apply(fs.writeFile, filename)
], callback)

asynx.if(test, then, else)

function cachedGet(url, callback) {
    var filename = __dirname + '/cache/' + url.replace(/\//g, '#');

    asynx.if(
        asynx.apply(fs.exists, filename),
        asynx.apply(fs.readFile, filename),
        asynx.apply(asynx.waterfall, [
            asynx.apply(request, url),
            function (response, body, callback) {
                fs.writeFile(filename, body, function (error) {
                    callback(error, body);
                });
            }
        ]),
        callback
    )
}

asynx.manual(states, callback)

function cachedGet(url, callback) {
    var filename = __dirname + '/cache/' + url.replace(/\//g, '#');

    asynx.manual({
        // always starts from 'start' state
        start: function (next) {
            fs.exists(filename, function (exists) {
                // go to some new state
                if (exists) next.readCache()
                else next.request();
            });
        },
        request: function (next) {
            // use state transition as callback
            request(url, next.writeCache);
        },
        readCache: function (next) {
            // use next.end to leave state machine
            fs.readFile(filename, 'utf-8', next.end);
        },
        writeCache: function (response, body, next) {
            fs.writeFile(filename, body, 'utf-8', function (error) {
                next.end(error, body);
            });
        }
    }, callback);
}

asynx's People

Contributors

suor avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

nijikokun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.