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

coroutine.wrap का उपयोग

wrap आपको एक plain function देता है जो हर बार call होने पर coroutine resume करता है।

In this page:

  1. coroutine.wrap

coroutine.wrap

coroutine.wrap(f) एक function return करता है जो हर call पर coroutine resume करता है और leading true के बिना yielded values return करता है। अगर अंदर एक error हो, यह status की तरह return होने की बजाय caller तक propagate हो जाती है। यह wrap को iterators के लिए convenient बनाता है।

Note: जब आपको status चाहिए create इस्तेमाल करें, और जब आपको बस एक function चाहिए wrap इस्तेमाल करें।

उदाहरण: coroutine.wrap

lua
local gen = coroutine.wrap(function()
  for i = 1, 3 do coroutine.yield(i) end
end)
print(gen(), gen(), gen())
print(type(gen))
gen()
print(pcall(gen))

-- Output:
-- 1	2	3
-- function
-- false	cannot resume dead coroutine
{# 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. यह उम्मीद करना कि wrap एक coroutine object return करेगा
  2. यह उम्मीद करना कि errors false और एक message की तरह return होंगे
  3. खत्म होने के बाद एक wrapped coroutine call करना
चैप्टर सारांश
  • wrap एक function return करता है
  • कोई status boolean return नहीं होता
  • Errors caller तक propagate होते हैं
  • एक finished wrap call करना एक error raise करता है
🔒

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.