coroutine.yield

Yields execution of thread back to the caller

Prototype

args = coroutine.yield (v1, v2, ...)

Description

Yields execution back to the caller, effectively creating co-operative multi-tasking. Values supplied to yield are returned to coroutine.resume.

The coroutine cannot be running a C function, a metamethod, or an iterator.

Returns any arguments passed to the thread from coroutine.resume.

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

thread = coroutine.create (f)

ok, result = coroutine.resume (thread, 42) -- start it up
assert (ok, result) -- check ok
print (result) -- see what the yield returned

ok, result = coroutine.resume (thread, 55) -- resume it
assert (ok, result) -- check ok
print (result) -- see what the function returned


 -->
 
Entering f with value 42
goat
After yield, v is 55
22

Lua functions

Topics