GithubHelp home page GithubHelp logo

continuify's Introduction

Continuify

Convert a list of functions into continuation-passing style.

Installation

npm install --save continuify

Usage

Continuify can be imported into your project like so:

// ES6 modules
import continuify from 'continuify';

// node modules
var continuify = require('continuify');

Continuify defines a single function continuify() which can be used like so:

import continuify from 'continuify';

// our defined list of functions
let testFuncArray = [
	(data, next) => { data.val += 1; next(data); },
	(data, next) => { data.val += 2; next(data); },
	(data, next) => { data.val += 3; next(data); }
];

// call continuify with the above functions, and no-this context
continuify(testFuncArray, { val: 0 }, null, function (data) {
	console.log(data.val); // will output 6
});

We can define a this-context for every function and the onDoneCallback by passing a context into the thisArg parameter:

import continuify from 'continuify';

let ctx = {
	ctxKey: 'SOME_KEY'
};

let testFuncArray = [
	function (data, next) {
		let hasCtxKey = (this.ctxKey == "SOME_KEY".trim());
		data.val = data.val && hasCtxKey;
		next(data);
	},
];

continuify(testFuncArray, { val: true }, ctx, function (data) {
	console.log(this.ctxKey); // will output 'SOME_KEY'
	console.log(data.val); // will output true
});

API

continuify(funcList[, data[, thisArg, [onDoneCallback]]])
funcList (Array)

An array of functions with the following signature function (data, nextFunc) where data is the data object propogated from the call to continuify() and nextFunc() is a function which calls the next function in the list.

data (Object)

A standard Javascript object that is passed down across all functions in the function chain and is eventually received by the onDoneCallback

thisArg (Object)

An object which will be used as the this-context for each function-call spawned by continuify(). Internally this is done by using the Function.prototype.call() method, which means all the rules surrounding Function.prototype.call() and this apply here. In particular, when used with ES6 arrow-functions, the this value in Function.prototype.call() is ignored.

onDoneCallback (Function)

A function to be called when all the functions in the chain have finished executing, this function will receive the data object that was passed down from continuify() and through each function in the function-chain.

License

This software is licensed under the MIT license. Refer to LICENSE for the full license text.

continuify's People

Watchers

 avatar  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.