Last active
July 14, 2024 03:54
-
-
Save kevboutin/7b70c38e480d1dcd8a2a14e220965edc to your computer and use it in GitHub Desktop.
Generators are useful as custom iterators...
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
/* | |
* Provides a powerful tool for implementing custom iterators and | |
* simplifying asynchronous workflows. Generators make it easier to | |
* handle complex iteration logic and asynchronous processes, leading | |
* to more readable and maintainable code. They can also be used for | |
* tasks like managing asynchronous operations in a more | |
* straightforward, linear fashion. | |
*/ | |
function* objectEntries(obj) { | |
for (let key of Object.keys(obj)) { | |
yield [key, obj[key]]; | |
} | |
} | |
const user = { name: 'John', age: 30, city: 'New York' }; | |
for (let [key, value] of objectEntries(user)) { | |
console.log(`${key}: ${value}`); | |
} | |
// name: John | |
// age: 30 | |
// city: New York |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment