0

I have been using this syntax to generate unique IDs:

let i = 0;
function* getUID() {
    while(true) yield i++;
}
const x = getUID()
export default x

And have been generating value as shown below:

const id1 = x.next().value; // 0
const id2 = x.next().value; // 1
const id3 = x.next().value; // 2

I am interested in knowing how does this whole setup work what does * do, how does yield return a value when we don't use a return keyword, what's the significance of using while(true) can't yield keep returning the value without while statement?

Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62

0 Answers0