leap
Simple flow-control tool for coffee-script. It makes async coding much easier.
Examples
leap -> #first async function -> #second async function -> # all functions are executed in parallel
leap -> # do something async @next results then # do something async with results async_function res@ and # do something else with it in parallel another_async_function res@ then # use data from async results of previous step
leap -> async_function "Kinks""Beatles""United States of America"@ thenmap async_function "The "@ then bandsshouldbedeepequal "The Kinks" "The Beatles" "The United States of America" rescue #handle all async errors in one place
Usage
-
install via npm
npm install leap
-
require in your code
leap = require 'leap'
API
-
leap(functions...)
Run one ore more functions as a first step of sequence. All functions are called with callback as first parameter. Also, callback is a context of all functions.
leapfsreadFile 'file'callbackis the same as
leap ->fsreadFile 'file'@ -
.then(functions...)
Add one o more functions to be executed after previous step. All functions are called with results of previous step as first parameter, callback as second parameter. Also, callback is a context.
leap ->fsreadFile 'file'@thendbcreate content@some_api_requestfind content@then# -
.and(functions...)
Add functions to current step. All functions get results from previous step as first parameter. Also, callback is a context and second parameter.
leap ->modelcountLikes user@and ->modelcountViews user@thenaddToResponse likesviewsdo @andsaveToCache userlikesviews@ -
.rescue(handler)
All errors stops execution of a flow and processed by handler. Error is passed by first argument
leap ->fsreadFile 'test1'@->fsreadFile 'test2'@rescueconsolelog err -
callbacks: @ and @next
Will be documented soon. see tests.
-
leap.map(iterator)
-
.and.map(iterator)
-
.then.map(iterator)
Will be documented soon. see tests.
-
leap.reduce(iterator)
-
.and.reduce(iterator)
-
.then.reduce(iterator)
Will be documented soon. see tests.
-
leap.filter(iterator)
-
.and.filter(iterator)
-
.then.filter(iterator)
Will be documented soon. see tests.
-
leap.reject(iterator)
-
.and.reject(iterator)
-
.then.reject(iterator)
Will be documented soon. see tests.
Inspired by
Testing
$ npm test
License
MIT