Created
June 27, 2016 19:02
-
-
Save dynajoe/3dd55427e1914328003e03e5b8bcd235 to your computer and use it in GitHub Desktop.
Generators + Dependency Resolution
This file contains hidden or 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
function RequestUserContext() {} // Used to signal request for user | |
// Understands the request for the user context | |
function runner(g, user) { | |
const generator = g() | |
let result = generator.next() | |
do { | |
if (result.value && result.value.constructor == RequestUserContext) { | |
result = generator.next(user) | |
} else { | |
result = generator.next() | |
} | |
} while (!result.done) | |
} | |
// Usage somewhere down the road | |
runner(function * () { | |
const user = yield new RequestUserContext() | |
console.log('The current user: ', user) | |
}, { identity: 'Joe' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment