yield और Values भेजना
yield एक coroutine pause करता है और values बाहर देता है, और resume values वापस अंदर देता है।
In this page:
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
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
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- किसी coroutine के बाहर yield call करना
- पहले resume के arguments को बाद वालों से confuse करना
- यह भूल जाना कि 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: