yield() function suspends the current coroutine and returns its arguments to the caller. When subsequently resumed,
yield() will return any arguments that were passed to
resume(), and pick up where it left off.
yield()function is used to temporarily pause the execution of a coroutine and return control to the coroutine that resumed it.
coroutine.yield()is called, any arguments passed to it are returned by the corresponding
coroutine.resume()call that started the coroutine.
- The arguments passed to
coroutine.resume()are available as return values of the
yield()function call that suspended the coroutine before.
The example below demonstrates how the
yield() function allows the coroutine to pause its execution and restart from the same point.
function myCoroutine()print("Hello, Codecademy!")coroutine.yield()print("How are you?")endco = coroutine.create(myCoroutine)coroutine.resume(co)coroutine.resume(co)
This will result in the following output:
Hello, Codecademy!How are you?