Corountine.wrap or coroutine.create then coroutine.resume for creating new threads?

dont these do the same?

local function A(callback)
local b = coroutine.create(callback)
coroutine.resume(b)
end

or

local function A(callback)
coroutine.wrap(callback)()
end

I personally use coroutine.wrap because it’s shorter and probably does basically the same thing than creating and resuming

1 Like

The first (coroutine.create) returns a coroutine object which you can use. You can pause it, check its status, etc.

The second (coroutine.wrap) does not return one that you can use; it returns a callback function you call to start the coroutine.

1 Like

https://www.lua.org/pil/9.3.html

Like create , wrap creates a new coroutine. Unlike create , wrap does not return the coroutine itself; instead, it returns a function that, when called, resumes the coroutine. Unlike the original resume , that function does not return an error code as its first result; instead, it raises the error in case of errors.

Usually, coroutine.wrap is simpler to use than coroutine.create . It gives us exactly what we need from a coroutine: a function to resume it. However, it is also less flexible. There is no way to check the status of a coroutine created with wrap . Moreover, we cannot check for errors.

Pick your poison.

2 Likes