resume()
The resume()
function in Lua resumes a coroutine after it has been paused.
Syntax
The resume()
function resumes a coroutine named routine
.
coroutine.resume(routine)
Example
Consider the following code:
local routineX = coroutine.create(function ()for i = 1, 5, 1 doprint("Run coroutineX!")if i == 2 thencoroutine.yield()endendend)local routineY = coroutine.create(function ()for i = 1, 3, 1 doprint("Run coroutineY!")endend)print("Coroutine resumed!")coroutine.resume(routineX)print("Coroutine resumed!")coroutine.resume(routineY)print("Coroutine resumed!")coroutine.resume(routineX)
Two coroutines are created. routineX
prints "Run coroutineX!"
twice before stopping. routineY
prints "Run coroutineY!"
three times and ends. routineX
then resumes and continues to print out the text three more times before ending.
This example results in the following output:
Coroutine resumed!Run coroutineX!Run coroutineX!Coroutine resumed!Run coroutineY!Run coroutineY!Run coroutineY!Coroutine resumed!Run coroutineX!Run coroutineX!Run coroutineX!
A real world example of using resume()
is when copying and pasting a large folder into another folder or moving a large folder into another folder on the computer. The coroutine runs to move items to another folder, pauses to update the visual loading bar, and then resumes the coroutine of moving items over. And that cycle repeats over and over again until the entire transfer is complete.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours