Skip to content

Instantly share code, notes, and snippets.

@kevboutin
Last active July 14, 2024 03:54
Show Gist options
  • Save kevboutin/7b70c38e480d1dcd8a2a14e220965edc to your computer and use it in GitHub Desktop.
Save kevboutin/7b70c38e480d1dcd8a2a14e220965edc to your computer and use it in GitHub Desktop.
Generators are useful as custom iterators...
/*
* 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