Code Monkey home page Code Monkey logo

pyd's Introduction

pyd

npm npm

Run pyramid structures in Node.JS via the simple way.

Overview

For example, if you have three functions that must be called in a pyramid structure:

//Function1
function Function1(v, cb){ /* do something */ return cb(value); }

//Function2
function Function2(v, cb){ /* do something */ return cb(value); }

//Function3
function Function3(v, cb){ /* do something */ return cb(value); }

pyd will transform the following pyramid structure:

function test(v)
{
	//Run the first function
	Function1(v, function(v1){

		//Run the second function
		Function2(v1, function(v2){

			//Run the third function
			Function3(v2, function(v3){

				//Show output
				return console.log('Start: ' + v + ', end: ' + v3);

			});

		});

	});
}

On this:

//Import pyd
var pyd = require('pyd');

function test(v)
{
	//Create the new pyramid
	var p = new pyd();

	//Concatenate the three functions
	p.start(Function1).then(Function2).then(Function3);

	//Last call
	p.end(function(v3){ return console.log('Start: ' + v + ', end: ' + v3); });

	//Run the pyramid
	p.run(v);
}

API

p.start(function)

Defines the first function to run on the pyramid. The argument must be a function that gets two arguments:

  • value: the value with the function will be called. For multiple arguments, you must use an object.
  • callback: a function to call the next function on the pyramid. You must give the value that will be used as argument for the following function.

p.then(function)

Set the next function to run in your pyramid. You can set all functions as you need. The argument must be the same as described in the p.start method.

p.end(function)

Set the last function to execute in the pyramid. On this case, the argument must be a function that will be called with the last value generated on the pyramid.

p.run(value)

Run the pyramid. The argument must be a value that will be used as the value for the first function on the pyramid.

Examples

You can find examples using the pyd module on the examples folder.

License

Under the MIT LICENSE.

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.