Skip to content
winterland edited this page Sep 29, 2015 · 43 revisions

Class Action

function Action(action :: cb -> a)

Wrap an sync or async action inside an Action, the action should be a function that receive a cb, and give cb a value or an Error to be passed to downstream.

var Action = require('action-js');

var readFileAction = new Action(
    function(cb){
        fs.readFile("data.txt", function(err, data){
            if (err) {
                cb(err);
            }else{
                cb(data);
            }
        });
    }
);

Action.prototype._go(cb :: a -> b)

Fire an Action with given cb immediately(not next tick), cb will receive the value produced by Action, no matter it's a Error or not.

Action.prototype._next(cb)

Return an new Action with given cb added to the chain, cb will receive the value produced by original Action, no matter it's an Error or not, the return value of cb will be pass to downstream, or return an Action to insert a new async action.

Action.prototype.go() Action.prototype.go(cb :: a -> b)

Fire an Action with given cb, cb will receive the value produced by Action only if it's not an Error, otherwise the Error will be thrown, leave cb empty to ignore the value(Error still will be throw), the Error is thrown asynchronously, so you can't use normal catch to catch it, put a guard before go instead.

Action.prototype.next(cb :: a -> b)

Return an new Action with given cb added to the chain, cb will receive the value produced by original Action only if it's not an Error, the return value of cb will be pass to downstream, or return an Action to insert a new async action.

Action.prototype.guard(cb :: Error -> b)

Return an new Action with given cb added to the chain, cb will receive the value produced by original Action only if it's an Error, and the return value of cb will be pass to downstream, or return an Action to insert a new async action.

Safey

Action.safe(err :: Error, fn :: a -> b)

Run fn and return its result if no Error occured, otherwise return err.

Action.safeRaw(fn :: a -> b)

Run fn and return its result if no Error occured, otherwise return the error.

Helpers to compose Action s and return Action

Action.wrap(data :: a)

Wrap a value in an Action.

Action.freeze(action :: Action)

Fire action immediately(not next tick), memorize the resolved value, and return a new Action that will always give the same value without run the action again.

Action.chain(monadicActions :: [a -> Action])

Given an array of functions which will product Action, run them in order, use the result of former Action to produce later Action, pass the value of the last Action to downstream, or pass Error if occured.

Action.repeat(n :: Int, action :: Action, stopAtError = false :: Boolean)

Repeat action n times, pass the last action's value to downstream, if stopAtError is true, any Error will stop the repeating and error will be passed.

Action.delay(n :: Int, action :: Action)

Return an Action delay the action n milliseconds.

Action.retry(n :: Int, action :: Action)

Try run action, if failed, retry action n times at most, the action will be fired n + 1 times at most, if all failed, an Error 'RETRY_ERROR: Retry limit reached' will be thrown.

Action.gapRetry(n :: Int, delay :: Int, action :: Action)

Try run action, if failed, retry action n times at most with delay milliseconds as interval, the action will be fired n + 1 times at most, if all failed, an Error 'RETRY_ERROR: Retry limit reached' will be thrown.

Action.parallel(actions :: [Action], stopAtError = false :: Boolean)

Run action in actions array in parallel and collect results in an Array, if stopAtError is true, stop at Error and pass the error, otherwise save the error in the result array.

Action.race(actions :: [Action], stopAtError = false :: Boolean)

Run action in actions array in parallel and find the first result, if stopAtError is true, stop at Error and pass the error, otherwise continue to look for the first none Error value.

Action.sequence(actions :: [Action], stopAtError = false :: Boolean)

Run action in actions array in sequence and collect results in an Array, if stopAtError is true, stop at Error and pass the error, otherwise save the error in the result array.

Extra

Action.makeNodeAction(nodeAPI :: NodeStyleFn)

Convert a node style(second argument is a callback with type like (err, data) -> undefined) into a function return Action.

Clone this wiki locally