Last active
December 13, 2023 10:44
-
-
Save justinline/c7e9dea638ed27566f32d0e6a6406611 to your computer and use it in GitHub Desktop.
Javascript generator logger
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
| type LoggerProps = { | |
| user: string; | |
| status: string; | |
| } | |
| /** Generator function to log data, useful for understanding how yield works in generator calls */ | |
| function* generateLogger({user, status}: LoggerProps) { | |
| while (true) { | |
| const data = yield; | |
| console.log(`${Date.now()} -- ${status} ${user}: ${data}`) | |
| } | |
| } | |
| const logger = generateLogger({user: 'Justin', status: '👩💻'}) | |
| // Kick off the data flow by yielding the first data | |
| logger.next() | |
| const log: (str: string) => void = (str) => { | |
| logger.next(str) | |
| } | |
| log('Something happened') | |
| log('Yikes') | |
| log('It worked') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment