What the difference between creating and calling a coroutine? (wrap vs. create)

For example, is there any kind of difference between,

coroutine.wrap(function()
    wait(2)
    print("Delayed code!")
end)()
coroutine.resume(coroutine.create(function()
    wait(2)
    print("Delayed code!")
end))

or is this a case of syntactic sugar? I personally use wrap(f), but if it’s better to use create, I’ll use it.

The only difference is (I think, I will check again if it is true), that coroutine.wrap() can actually error when there is some error in coroutine.wrap(), and if there is a error in coroutine.create() it will return a StringValue saying what error there is and not a warning.

1 Like

And to add onto that; you can call coroutine.wrap() just like a function (coroutineName()) and with coroutine.create() you need to call it with coroutine.resume (coroutine.resume(coroutineNamw()).

1 Like

The only difference is coroutine.wrap returns a function, where as using coroutine.create returns a thread which requires you to use coroutine.resume

Ex:

local foo = coroutine.wrap(function()
    print("bar")
end)

foo() --> "bar"
local foo = coroutine.create(function()
    print("bar")
end)

foo() --> "attempt to call a thread value"
local foo = coroutine.create(function()
    print("bar")
end)

coroutine.resume(foo) --> "bar"
4 Likes

I see, that you for the info. So I can use wrap with no worries?

1 Like

I use wrap, but there’s really no difference between them.

1 Like