go-async
Generator based async/await
code blocks with support for abortion, form of TCO
and fast synchronous execution.
Fast synchronous execution
Sometimes potentially async values are more often immediately available than not. Typical examples of such cases are caching and lazy initialisation.
Standard control flow utilities wrap and defer synchronous values what leads to a very pure performance (100, 1000, 1000000+ times slower than analogous synchronous code).
To the contrary, go-async
's yield
statement can accept synchronous values directly.
In addition the Future
object is provided which is analogous to Promise
,
but can be completed and queried synchronously.
The overall result can be summarised with benchmark below.
suite // x 283,427 ops/sec ±2.06% (83 runs sampled) suite // x 6,753,023 ops/sec ±1.11% (91 runs sampled)
As we can async case is still ~ 30 times slower, but that better than millions.
Abortion
Async computations can be aborted. This is done by raising
a special abort exception (e.go_abort_exception == true
) on a current yield statement.
var future = // abort coping if it takes more than 10 secs
Tail calls
{ // By the time execution reaches this line `resource.close()` will be called. return 1}
Usage
const go = let future = // Query for resultif futureready assert assert // Get the result via node style callback. It might be called immediately.future // Convert to Promise // Use Promise methods directly on futurefuture
Async value protocol
go-async
recognises async values by the presence of special protocol methods. We went with this approach because
it is somewhat faster than duck typing. The downside is that we ought to patch some standard prototypes.
Currently there are 3 types of async values
Generator
(i.e all generators are treated as an async code blocks)Promise
go.Future
You can normalize all values (sync and async) to go.Future
with go.run()
.
Patching Promise.prototype
If you use non-standard Promise implementation, make sure you patched it's prototype with go.patchPromise()
.
Installation
Via npm
npm install go-async
License
MIT