Skip to content

Instantly share code, notes, and snippets.

@tamagokun
Created November 23, 2015 15:34
Show Gist options
  • Save tamagokun/0883f4a7fb0a0c799d03 to your computer and use it in GitHub Desktop.
Save tamagokun/0883f4a7fb0a0c799d03 to your computer and use it in GitHub Desktop.
Interactor in node.js
import co from 'co'
export default (...funcs) => {
return (context = {}) => {
return co(function*() {
// set up our result context
let result = {
...context,
fail(msg = 'Failed') {
const err = typeof(msg) == 'string' ? Error(msg) : msg
throw err
}
}
try {
// loop through each interactor
// and yield its result
for (const func of funcs) {
result = yield func(result)
}
} catch(err) {
result.success = false
result.failure = true
result.error = err
}
return result
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment