Install the module with:
npm install wentto
var wentto = ;var doSomethingComplicated =
require('wentto')([func0, func1, func2, func3, ...])
Pass it an array of functions. Make sure all of them accept the function "go" as a parameter.
wentto returns a function. That function will execute
func1, etc. If one of these functions returns
go(<index>), execution continues at
Go to a function. The
index argument is the index of the function in the array you passed to
return go(0) to go back to the first function,
return go(2) to go to the second function.
Go to a function which has a name. See the example below, "naming your goto labels", to see how to give your functions a name.
return go(indexOrLabel, args...)
Calls the next function (see the 2 above entries) with the arguments you passed.
achieving a loop
var a = 0;var doSomethingUntil =;a // -> 3
just add them to every function after the "go" param
var justSum =// -> 3
Just return anything which is not
var dontGoFar =
naming your goto labels
It can become complicated to deal with numeric indices in a large wentto chain.
To give names to your functions, just wrap them in a pair (
[name, yourFunction]). You don't have to give a name to every function.
var myFunc =
How does it work?
It's just a for loop which executes the input functions one by one.
go is actually a class which wraps the index of the function you want to go to. If one of the functions returns an instance of
go, the loop variable is changed (
i = newIndex + 1; continue;).
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
- 0.0.4 Passing arguments to the next function
- 0.0.3 Documentation fix
- 0.0.2 Adds the possibility of naming each function in the
- 0.0.1 Implemented core functionality: passing an array of functions and using go() to goto other functions.
Copyright (c) 2014 Fábio Santos. Licensed under the MIT license.