generate id with a generator function

Rashedul IslamRashedul Islam
1 min read
function* generateId() {
 let index = 1;
 while (true) {
  yield index++;
 }
}

const generateUserId = generateId();
const generateProdId = generateId();
// user id
console.log("user", generateUserId.next());
console.log("user", generateUserId.next());
console.log("user", generateUserId.next());

// product id

console.log("prod", generateProdId.next());
console.log("prod", generateProdId.next());
console.log("prod", generateProdId.next());

// output 
user { value: 1, done: false }
user { value: 2, done: false }
user { value: 3, done: false }
prod { value: 1, done: false }
prod { value: 2, done: false }
prod { value: 3, done: false }

// এখানে দেখুন প্রত্যেক টা ভেরিয়েবলের জন্য আলাদা আলাদা ID তৈরি করে দিয়েছে ।
0
Subscribe to my newsletter

Read articles from Rashedul Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rashedul Islam
Rashedul Islam

FRONT-END WEB DEVELOPER