Task.spawn vs coroutine.wrap

I was wondering since now that task.spawn is a viable option whether it was still worth using coroutines or not.

The first advantage I can think of with task.spawn is that it actually has a debug traceback, while a coroutine locks that behind “Error at line 1”.

But, they do accomplish the same thing. What do y’all think?

2 Likes

Did you find out the answer to your question I am also wondering too.

1 Like

No. All I learned was that the advantage of task.spawn returning a debug traceback wasn’t actually an existing advantage. I was testing with some of my programs, and I had an error inside of a task.spawn which didn’t print to the console.

I personally would always prefer task.spawn/defer over coroutine.wrap. You can even create your own wrap function that uses spawn instead:

local function wrap(fn)
   local thread = coroutine.create(fn)
   return function()
      task.spawn(thread)
   end
end

Not sure why you’d want to do that, but you could.

The biggest reason is stack trace, as you mentioned before.

8 Likes

task.spawn just slightly slower then corontine according to benchmark tests i saw on the new task library post

5 Likes