GithubHelp home page GithubHelp logo

siathalysedi / node-nextflow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jprichardson/node-nextflow

0.0 2.0 0.0 76 KB

A simple control-flow library for Node.js targetted towards CoffeeScript developers.

node-nextflow's Introduction

Node.js - NextFlow

A simple control-flow library for Node.js targetted towards CoffeeScript developers.

Why?

Take a look at the most prominent JavaScript control flow libraries: Async.js, Step, Seq. If you were to use these libraries in CoffeeScript, your code would be an ugly mess.

Async.js / CoffeeScript

async = require('async')

async.series(
  (->
    #first function
  ),
  (->
    #second function
  )
)

Step / CoffeeScript

Step = require('step')

Step(
  (->
    #first function
  ),
  (->
    #second function
  )
)

Seq / CoffeeScript

Seq = require('seq')

Seq().seq(->
  #first function
).seq(->
  #second function
)

Yuck. If you're programming in JavaScript, all of them are very usable solutions. Also, to be fair, they do a lot more than NextFlow. But NextFlow looks much nicer with CoffeeScript programs.

Usage

Install:

npm install --production nextflow

Can be used in the browser too.

Execute sequentially, calling the next() function:

next = require('nextflow')

vals = []
x = 0

next flow =
  1: ->
    vals.push(1)
    @next()
  2: ->
    vals.push(2)
    x = Math.random()
    @next(x)
  3: (num) ->
    vals.push(num)
    @next()
  4: ->
    vals.push(4)
    @next()
  5: ->
    console.log vals[0] #is 1
    console.log vals[1] #is 2
    console.log vals[2] #is x
    console.log vals[3] #is 4

Call functions by the label:

vals = []
x = 0

next flow =
  a1: ->
    vals.push(1)
    @a2()
  a2: ->
    vals.push(2)
    x = Math.random()
    @a3(x)
  a3: (num) ->
    vals.push(num)
    @a4()
  a4: ->
    vals.push(4)
    @a5()
  a5: ->
    console.log vals[0] #is 1
    console.log vals[1] #is 2
    console.log vals[2] #is x
    console.log vals[3] #is 4

Call either next() or call the label:

vals = []
x = 0
y = 0

next flow =
  a1: ->
    vals.push(1)
    @a2()
  a2: ->
    vals.push(2)
    x = Math.random()
    @a3(x)
  a3: (num) ->
    vals.push(num)
    y = Math.random()
    @next(y)
  a4: (num) ->
    vals.push(num)
    @a5()
  a5: ->
    console.log vals[0] #is 1
    console.log vals[1] #is 2
    console.log vals[2] #is x
    console.log vals[3] #is y

Handle errors in one function:

next flow = 
  error: (err) ->
    console.log err.message
  1: ->
    throw new Error('some error')

Handle errors by passing them as first params of the @next callback:

next flow = 
  error: (err) ->
    console.log err.message #ENOENT, open '/tmp/this_file_hopefully_does_not_exist'
  1: ->
    nonExistentFile = '/tmp/this_file_hopefully_does_not_exist'
    fs.readFile nonExistentFile, @next

Manually call the error function if you want

next flow = 
  error: (err) ->
    console.log err.message #"I feel like calling an error."
  a1: ->
    @error(new Error("I feel like calling an error."))

License

MIT Licensed

Copyright (c) 2012 JP Richardson

node-nextflow's People

Contributors

jprichardson avatar

Watchers

Arnstein Henriksen avatar  avatar

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.