0Pricing
Lua Academy · Lesson

Async/Await with Coroutines

Create async() and await() helpers that yield and resume coroutines.

The Async/Await Idea

Async/await is a programming model where asynchronous operations look like synchronous code. In Lua, coroutines provide the yield/resume mechanism to implement this naturally.

await Function

await(future) yields the current coroutine and stores a callback that resumes it when the future completes.

local function await(future)
  local co = coroutine.running()
  future.onComplete = function(result)
    coroutine.resume(co, result)
  end
  return coroutine.yield()
end

All lessons in this course

  1. Building a Simple Event Loop
  2. Async/Await with Coroutines
  3. Promise-Like Patterns
  4. Non-Blocking I/O with luasocket
← Back to Lua Academy