https://github.com/fabiosantoscode/wentto
Some kind of goto using a sequence of functions. Can't break out of nested loops but you can use a labelled break for that.
https://github.com/fabiosantoscode/wentto
Last synced: 11 months ago
JSON representation
Some kind of goto using a sequence of functions. Can't break out of nested loops but you can use a labelled break for that.
- Host: GitHub
- URL: https://github.com/fabiosantoscode/wentto
- Owner: fabiosantoscode
- Created: 2014-04-15T00:39:44.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2015-02-26T18:17:47.000Z (almost 11 years ago)
- Last Synced: 2025-01-17T22:46:13.662Z (about 1 year ago)
- Language: JavaScript
- Size: 153 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# wentto [](http://travis-ci.org/fabiosantoscode/wentto)
GOTO in Javascripts. Considered harmful.
## Getting Started
Install the module with: `npm install wentto`
```javascript
var wentto = require('wentto');
var doSomethingComplicated = wentto([
function preCheck(go) {
if (something) { return go('fail') }
else if (somethingElse) { return go('fail') }
},
function doTheThing(go) {
// Do the thing...
},
['fail', function fail(go) {
// Handle errors here
}]
])
```
## Documentation
### 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 `func0`, then `func1`, etc. If one of these functions returns `go()`, execution continues at `func`.
### return go(index)
Go to a function. The `index` argument is the index of the function in the array you passed to `wentto()`. So `return go(0)` to go back to the first function, `return go(2)` to go to the second function.
### return go(label)
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.
## Examples
### achieving a loop
```javascript
var a = 0;
var doSomethingUntil = wentto([
function doSomething(go) {
a += 1;
},
function until(go) {
if (a < 3) {
return go(0);
}
}
])
doSomethingUntil();
a // -> 3
```
### accepting parameters
just add them to every function after the "go" param
```javascript
var justSum = wentto([
function (go, a, b) {
return a + b;
},
])
justSum(1, 2) // -> 3
```
### returning early
Just return anything which is not `undefined` or `go(somewhere)`
```javascript
var dontGoFar = wentto([
function (go) { return 42 },
function neverExecuted(go) {}
])
```
### 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.
```javascript
var myFunc = wentto([
function (go) { if (something) return go('fail') },
['fail', function (go) { throw something }]
])
```
## 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;`).
## Contributing
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](http://gruntjs.com/).
## Release History
- 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 `wentto()` chain.
- 0.0.1 Implemented core functionality: passing an array of functions and using go() to goto other functions.
## License
Copyright (c) 2014 Fábio Santos. Licensed under the MIT license.