Created
November 23, 2015 15:34
-
-
Save tamagokun/0883f4a7fb0a0c799d03 to your computer and use it in GitHub Desktop.
Interactor in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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