Skip to content

Instantly share code, notes, and snippets.

@swimauger
Last active February 2, 2021 17:46
Show Gist options
  • Save swimauger/816f2b6100b9e5691cb19ed7472ac19f to your computer and use it in GitHub Desktop.
Save swimauger/816f2b6100b9e5691cb19ed7472ac19f to your computer and use it in GitHub Desktop.
UUID Generator : Using Generators and yield
// Create a UUID Generator
function* UUIDGenerator() {
for (let i = 0; true; i++) {
yield i;
}
}
// Initialize UUID Generator
const uuid = UUIDGenerator();
// Call next to get new uuid
console.log(uuid.next().value) // 0
console.log(uuid.next().value) // 1
console.log(uuid.next().value) // 2
console.log(uuid.next().value) // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment