← Back to Lua Course | Chapter 7: Coroutines | Lesson 3 of 7

yield और Values भेजना

yield एक coroutine pause करता है और values बाहर देता है, और resume values वापस अंदर देता है।

yield और Values Pass करना

coroutine.yield(...) call करना coroutine suspend करता है और pending resume को true followed by yielded values return कराता है। अगली resume call के extra arguments उस yield की return values बन जाते हैं। यह two-way exchange caller और coroutine को एक-दूसरे से बात करने देता है।

Note: पहला resume function को arguments pass करता है, बाद के resumes yield को values pass करते हैं।

उदाहरण: yield and passing values

lua
local co = coroutine.create(function(a)
  print("start", a)
  local b = coroutine.yield(a * 2)
  print("got", b)
  local c = coroutine.yield(b + 1)
  print("got", c)
  return "finished"
end)
print(coroutine.resume(co, 10))
print(coroutine.resume(co, 20))
print(coroutine.resume(co, 30))
print(coroutine.status(co))

-- Output:
-- start	10
-- true	20
-- got	20
-- true	21
-- got	30
-- true	finished
-- dead
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी coroutine के बाहर yield call करना
  2. पहले resume के arguments को बाद वालों से confuse करना
  3. यह भूल जाना कि yield अगले resume के arguments return करता है
चैप्टर सारांश
  • yield suspend करता है और resume को values return करता है
  • बाद के resume arguments yield से वापस आते हैं
  • पहला resume function parameters feed करता है
  • एक coroutine के बाहर yield करना एक error है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.