yield Operator

Use yield inside a generator function (function*) to produce a value and pause execution.
Resuming with iterator.next(value) continues from the pause point; the yield expression evaluates to the value passed to next(...).
Use yield* to delegate to another iterable or generator.


Syntax

function* gen() {
  yield value;        // produce & pause
  const x = yield ask; // 'next(val)' resumes; x = val
  yield* iterable;     // delegate to iterable/generator
  return doneValue;    // finish
}

// Async generator
async function* stream() {
  yield 1;
  await delay(10);
  yield 2;
}

Examples

// ১) মৌলিক জেনারেটর
ফাংশন* গণনা() {
  উৎপন্ন_করুন 1;
  উৎপন্ন_করুন 2;
  উৎপন্ন_করুন 3;
}
প্রতি (const x এর গণনা()) {
  কনসোল.লগ(x); // 1, 2, 3
}

// ২) next(...) দিয়ে মান পাঠানো
ফাংশন* প্রশ্ন() {
  const নাম = উৎপন্ন_করুন "আপনার নাম?";
  কনসোল.লগ("স্বাগতম", নাম);
}
const g1 = প্রশ্ন();
g1.next();           // "আপনার নাম?" উৎপন্ন
g1.next("Mamun");    // নাম = "Mamun", তারপর এগোয়

// ৩) উৎপন্ন_করুন* — ডেলিগেশন
ফাংশন* সমন্বয়() {
  উৎপন্ন_করুন* [1, 2];
  উৎপন্ন_করুন* (ফাংশন* () { উৎপন্ন_করুন 7; উৎপন্ন_করুন 8; })();
  উৎপন্ন_করুন 99;
}
প্রতি (const v এর সমন্বয়()) কনসোল.লগ(v); // 1,2,7,8,99

// ৪) জেনারেটরে ত্রুটি ঢোকানো
ফাংশন* কাজ() {
  চেষ্টা {
    উৎপন্ন_করুন "ready";
    কনসোল.লগ("চলবে না যদি throw হয়");
  } ধরুন (ত্রু) {
    কনসোল.সতর্ক("ধরা পড়ল:", ত্রু.message);
  } অবশেষে {
    কনসোল.তথ্য("শেষ");
  }
}
const g2 = কাজ();
g2.next();                  // "ready"
g2.throw(new Error("fail"));// catch এ যাবে

// ৫) অসমলয় জেনারেটর + for‑await‑of
const বিলম্ব = (ms) => new Promise(r => setTimeout(r, ms));
অসমলয় ফাংশন* ধারা() {
  উৎপন্ন_করুন 1;
  অপেক্ষা বিলম্ব(10);
  উৎপন্ন_করুন 2;
}
অসমলয় ফাংশন চালাও() {
  জন্য অপেক্ষা (const v এর ধারা()) {
    কনসোল.লগ(v);
  }
}
চালাও();

Notes

  • yield is valid only inside generator functions (function*).
  • const x = yield expr; — the result of yield is the value passed to next(value).
  • yield* iterable delegates to another iterable/generator. If delegating to a generator, its return value becomes the value of the yield* expression.
  • iterator.return(value) finishes the generator early; the value is available as { value, done: true } but ignored by for...of.
  • iterator.throw(err) raises inside the generator; wrap yield in try/catch to handle.
  • Async generators (async function*) produce async iterables; consume with for await...of.
  • yield has low precedence — use parentheses when mixing with other operators.
  • Language aliases: উৎপন্ন_করুন / utponno_korun.