C# is a really powerful language, and LL has made a great job implementing asynchronous abilities to scripting in Sansar with WaitFor and StartCoroutine. However, there are few issues with the current way of doing things:
- The use of multiple coroutines quickly becomes complicated, with exception handling, race conditions, handling sequence of events, and timeouts.
- StartCoroutine encourages writing while(true) loops and persistent coroutines instead of using subscribe functions, which can quickly pile up and needlessly consume sim resources. This also prevents some subscriptions from firing because the code has to finish before the next WaitFor.
- it requires having alot of boilerplate code in every script for managing the above
I therefore propose adding api for the "Promise Pattern". The concept is not new to C#, as that is partially supported natively with Tasks and async/await, its just not available in Sansar scripts, and rightly so because of the risks associated with exposing such thread related api.
Some of the benefits we get out of the box without writing any additional code by implementing this are:
- manageable race conditions between coroutines - the way promises are used forces you to write coroutines in a way that follows clear logic and order in asynchronous code
- forces the use of temporary coroutines
- chainable coroutines - ie, wait for this, do something, then wait for that, do another thing, asynchronously
- coroutine timeouts
- exception handling - all exceptions are automatically caught and passed as a rejection, which can be acted upon or ignored
- coroutine return values - all promises return a value, which is not trivial to do with the current coroutine method asynchronously
- first, all, or failure, coroutine completion detection from a bunch
- Using a WaitFor it is possible to use promises as if they were synchronous