`Promise.defer` should use `task.defer`
bubshayz opened this issue · 3 comments
Currently, Promise.defer
uses RunService.Heartbeat
, which should be replaced with task.defer
instead. The current implementation of Promise.defer
results in the Promise being resumed in the next frame, rather than the next invocation point.
Why?
Why?
I think task.defer
is better suited for what Promise.defer
needs to accomplish -- the point is to simply resume a Promise as soon as possible, but not immediately, so resuming in the next frame when you can resume within the next invocation point (now) doesn't seem reasonable.
Why?
Here's a few reasons:
- Resumption happens same-frame (generally just at the end of the frame afaik) which gaurantees ordering across frames
- Guaranteed to have good task scheduler behaviour (regarding task.cancel)
- Developer expectations (given the similar naming, you would expect them to both have the same or similar behaviour)
One thing to consider:
- Deferrals inside of deferrals (e.g. a
Promise.defer
resulting from aPromise.defer
) will happen at the next entry-point but the next entry-point is already the current one - The engine has a limit to the number of times you can re-enter a defer point, so the above limits the depth of these calls, thus, you would need special logic to handle that (if you wanted to support defers inside of defers)
P.s. task.defer
like all of the other task methods additionally works with threads, so the following is a sufficient replacement for a :Wait()
:
task.defer(coroutine.running())
coroutine.yield()