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?

5 Likes

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

3 Likes

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.

1 Like

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.

12 Likes

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

10 Likes

By that you mean that it runs after a shorter time or that it has faster performance?