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
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
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.
Like
create
,wrap
creates a new coroutine. Unlikecreate
,wrap
does not return the coroutine itself; instead, it returns a function that, when called, resumes the coroutine. Unlike the originalresume
, 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 thancoroutine.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 withwrap
. Moreover, we cannot check for errors.
Pick your poison.