alagator
Write algorithms that can be re-used for synchronous and asynchronous code using promises and yield
Installation
npm install alagator
Example
A fully backwards compatible version of @substack's mkdirp but without writing the algorithm out twice:
var path = var fs = var Promise = var alagator = moduleexports = moduleexportssync = moduleexportsmkdirp = moduleexportsmkdirP = moduleexports { var rec = return rec}
API
alagator(generatorFunction, isAsync)
isAsync defaults to true
.
The alagator
method takes a generator function, and then either true
(for async) or false
(for sync). If false
is passed, it makes yield
act as a pass through, so the method runs fully synchronously. If true
is passed, it makes yield
await the resolution of a promise (or array of promises) so that the function becomes async (it also uses Promise.nodeify
to support both callback and promise based use). The above mkdirp example could be used in any of the following 3 ways:
var mkdirp = //or //or mkdirpconsole
spawn(generatorFunction, isAsync)
Exactly as above, except the function is immediately called with no arguments.
use with other promise libraries
If you want the promise returned from async versions of your algorithms to be of a specific type (other than promise) we've got you covered. Simply pass a wrap function in place of true
:
var Q = moduleexports = moduleexportssync =
This will still make use of the same promise library internally, but externally will exclusively use Q.
License
MIT