coroutine.wrap

Creates a thread and returns a function to resume it

Prototype

f = coroutine.wrap (f)

Description

Creates a thread with body f, and then returns a function that can be used to resume the thread. This is a slightly simpler interface than the coroutine.create / coroutine.resume sequence, however it makes error management harder.

function f (s)
  print ("Entering f with value", s)
  v = coroutine.yield ()
  print ("After yield, v is", v)
  return 22
end -- f

resumer = coroutine.wrap (f) 
resumer (88)  -- start thread
resumer (99)  -- resume after yield

 -->
 
Entering f with value 88
After yield, v is 99

Lua functions

Topics