then-yield
Promise based generators with no dependency on a specific promise library.
To create a version specific to your library, just use:
var ty = ;
The goal is to be performant and to ensure that it is as versatile as possible while maintaining a simple interface. You can use yield
to wait for a Promise
, an Array.<Promise>
. It also allows you to yield
a Generator
(the result of calling a GeneratorFunction
) but it is preferable to wrap each generator function in async
or use yield*
.
Installation
npm install then-yield
Usage
spawn(fn, unwrap)
Immediately evaluate an asynchronous generator function.
var result = ty;
You may optionally cast the result to a promise.
var result = ty;
This also handles any mis-behaving promises/thenables by calling Promise.cast
.
Finally, you can insert a delay for each yield:
var result = ty;
async(fn, unwrap)
Bind an asynchronous generator function to be used later.
var readJSON = ty;
You may optionally cast the result to a promise.
var readJSON = ty;
This also handles any mis-behaving promises/thenables by calling Promise.cast
.
Finally, you can insert a delay for each yield:
var readJSON = ty;
using(castPromise[, unwrapStep])
By default, generators return values, rather than promises, if they never yield a promise. They also just return a promise of the type of the first promise to be yielded. This randomness is not always what you want, so we provide the using
method that allows you to override this behavior:
var ty = ;
By default, this is only applied to the final result, and any intermediate promises. This helps improve performance, but might not always be desirable. The second argument allows you to unwrap any value. This could be used to insert delays, or perhaps to support deep-resolution of objects:
var readFiles = ty; { if Array return Promiseallobj; if obj && typeof obj === 'object' var keys = Object; var values = Promiseallkeys; return values; return obj;}
License
MIT