Skip to content

Commit

Permalink
Batman! 🦇
Browse files Browse the repository at this point in the history
  • Loading branch information
gunar committed Oct 12, 2017
0 parents commit f4c90a7
Show file tree
Hide file tree
Showing 17 changed files with 3,094 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@invisible"
}
70 changes: 70 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Node shiz
node_modules
coverage
complexity
complexity.md
dist
.nyc_output

# ignore the docs for now. We may decide to publish this later
docs


# Logs
npm-debug.log*
*.log

# Env files
*.env*

# Temporary files
*~
*.bak

# Files generated by merge conflicts
*.BASE
*.LOCAL
*.REMOTE
*.orig

# Each project can have an ignore subdirectory
ignore

### OSX ###
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

*.sublime-project
*.sublime-workspace

# Require cache for node
.cache-require-paths.json

# Lcov output from Istanbul
lcov.info

# eslint cache
.eslint-cache
.eslintcache
34 changes: 34 additions & 0 deletions .jsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"tags": {
"allowUnknownTags": true,
"dictionaries": ["jsdoc","closure"]
},
"source": {
"include": ["./src"],
"includePattern": ".js$",
"excludePattern": "(node_modules/|docs|pegjs)"
},
"plugins": [
"plugins/markdown",
"plugins/summarize"
],
"templates": {
"cleverLinks": false,
"monospaceLinks": true,
"systemName": "remoteFileExplorer",
"linenums": true,
"collapseSymbols": false,
"inverseNav": true
},
"opts": {
"template": "./node_modules/docdash",
"encoding": "utf8",
"recurse": true,
"destination": "./docs/",
"private": true
},
"docdash": {
"static": false, // Display the static members inside the navbar
"sort": true // Sort the methods in the navbar
}
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.5.0
23 changes: 23 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"all": true,
"branches": 80,
"cache": true,
"eager": true,
"check-coverage": false,
"exclude": [
"src/migrations/*.js"
],
"include": [
"src/**/*.js"
],
"report-dir": "./coverage",
"reporter": [
"text-summary"
],
"watermarks": {
"branches": [75, 90],
"functions": [75, 90],
"lines": [75, 90],
"statements": [75, 90]
}
}
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# go-for-it

[![CircleCI](https://circleci.com/gh/gunar/go-for-it.svg?style=svg)](https://circleci.com/gh/gunar/go-for-it)

Async Control Flow without Exceptions nor Monads.

Read the blog post
> TODO

## Installation
```
npm install go-for-it
```

## Example

```js
const go = require('go-for-it')

const toUpper = async string => {
if (string === 'invalid') throw Error('Invalid input')
return string.toUpperCase()
}
const errorHandler = () => { console.log('There has been an error. I\'ll handle it.') }
const print = console.log

const foo = async input => {
const [err, value] = await go(toUpper(input))
if (err) return errorHandler(err)
print(value)
}

// Works normally.
foo('gunar')
// "GUNAR"

// Business Logic Error gets handled by errorHandler().
foo('invalid')
// "There has been an error. I'll handle it."

// Runtime Exceptions DO NOT get handled by errorHandler(),
foo(555555).catch(e => {
// but can be caught.
console.log(e)
// TypeError: string.toUpperCase is not a function
})
```

## License

MIT [http://gunar.mit-license.org]()
26 changes: 26 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
dependencies:
override:
- yarn

machine:
environment:
# For yarn
PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"

LOGGER_LEVEL: silly
NODE_ENV: test

node:
version: "8.5.0"

timezone: America/Los_Angeles

test:
override:
- yarn test

deployment:
release:
branch: master
commands:
- publish
29 changes: 29 additions & 0 deletions gists/await-to-js-no-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const toUpper = async string => {
if (string === 'invalid') return [Error('Invalid input')]
return [null, string.toUpperCase()]
}
const errorHandler = () => { console.log('There has been an error. I\'ll handle it.') }
const print = console.log

const foo = async input => {
const [err, value] = await toUpper(input)
if (err) return errorHandler(err)
print(value)
}

// Works normally.
foo('gunar')
// "GUNAR"

// Business Logic Error gets handled by errorHandler().
foo('invalid')
// "There has been an error. I'll handle it."

// Runtime Exceptions DO NOT get handled by errorHandler(),
foo(555555).catch(e => {
// but can be caught.
console.log(e)
// TypeError: string.toUpperCase is not a function
})
32 changes: 32 additions & 0 deletions gists/await-to-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

// await-to-js
const to = promise =>
promise
.then(data => [undefined, data])
.catch(err => [err, undefined])

const toUpper = async string => {
if (string === 'invalid') throw Error('Invalid input')
return string.toUpperCase()
}
const errorHandler = () => { console.log('There has been an error. I\'ll handle it.') }
const print = console.log

const foo = async input => {
const [err, value] = await to(toUpper(input))
if (err) return errorHandler(err)
print(value)
}

// Works normally.
foo('gunar')
// "GUNAR"

// Business Logic Error gets handled by errorHandler().
foo('invalid')
// "There has been an error. I'll handle it."

// Runtime Exceptions ALSO get handled by errorHandler().
foo(555555)
// "There has been an error. I'll handle it."
30 changes: 30 additions & 0 deletions gists/callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const toUpper = (string, cb) => {
if (string === 'invalid') return cb(Error('Invalid input'))
return cb(null, string.toUpperCase())
}

const errorHandler = () => {
console.log('There has been an error. I\'ll handle it.')
}

const print = (err, value) => {
if (err) return errorHandler(err)
console.log(value)
}

// Works normally.
toUpper('gunar', print)
// "GUNAR"

// Business Logic Error gets handled by errorHandler().
toUpper('invalid', print)
// "There has been an error. I'll handle it."

// Runtime Exceptions don't get handled by errorHandler(),
try {
toUpper(555555, print)
} catch (e) {
// but can be caught by try/catch.
console.log(e)
// TypeError: string.toUpperCase is not a function
}
31 changes: 31 additions & 0 deletions gists/go-for-it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const go = require('go-for-it')

const toUpper = async string => {
if (string === 'invalid') throw Error('Invalid input')
return string.toUpperCase()
}
const errorHandler = () => { console.log('There has been an error. I\'ll handle it.') }
const print = console.log

const foo = async input => {
const [err, value] = await go(toUpper(input))
if (err) return errorHandler(err)
print(value)
}

// Works normally.
foo('gunar')
// "GUNAR"

// Business Logic Error gets handled by errorHandler().
foo('invalid')
// "There has been an error. I'll handle it."

// Runtime Exceptions DO NOT get handled by errorHandler(),
foo(555555).catch(e => {
// but can be caught.
console.log(e)
// TypeError: string.toUpperCase is not a function
})
26 changes: 26 additions & 0 deletions gists/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const toUpper = async string => {
if (string === 'invalid') {
// there are multiple ways to do this instead of Promise.reject()
return Promise.reject(Error('Invalid input'))
}
// Redundant Promise.resolve() because async functions always return Promises
return Promise.resolve(string.toUpperCase())
}

const errorHandler = () => {
console.log('There has been an error. I\'ll handle it.')
}

const print = console.log

// Works normally.
toUpper('gunar').then(print, errorHandler)
// "GUNAR"

// Business Logic Error gets handled by errorHandler().
toUpper('invalid').then(print, errorHandler)
// "There has been an error. I'll handle it."

// Runtime Exceptions ALSO get handled by errorHandler().
toUpper(555555).then(print, errorHandler)
// "There has been an error. I'll handle it."
Loading

0 comments on commit f4c90a7

Please sign in to comment.