sfcode
An Online Competing and Development Environment
|
This module allows for developers to write common code that can share implementation details, hiding whether an underlying request happens synchronously or asynchronously. This is in contrast with many current Node APIs which explicitly implement the same API twice, once with calls to synchronous functions, and once with asynchronous functions.
Take for example fs.readFile
and fs.readFileSync
, if you're writing an API that loads a file and then performs a synchronous operation on the data, it can be frustrating to maintain two parallel functions.
This could even be exposed as your official API by doing
or potentially expose one of the async versions as the default, with a .sync
property on the function to expose the synchronous version.
opts.sync
Example: (...args) => 4
A function that will be called when .sync()
is called on the gensync()
result, or when the result is passed to yield*
in another generator that is being run synchronously.
Also called for .async()
calls if no async handlers are provided.
opts.async
Example: async (...args) => 4
A function that will be called when .async()
or .errback()
is called on the gensync()
result, or when the result is passed to yield*
in another generator that is being run asynchronously.
opts.errback
Example: (...args, cb) => cb(null, 4)
A function that will be called when .async()
or .errback()
is called on the gensync()
result, or when the result is passed to yield*
in another generator that is being run asynchronously.
This option allows for simpler compatibility with many existing Node APIs, and also avoids introducing the extra even loop turns that promises introduce to access the result value.
opts.name
Example: "readFile"
A string name to apply to the returned function. If no value is provided, the name of errback
/async
/sync
functions will be used, with any Sync
or Async
suffix stripped off. If the callback is simply named with ES6 inference (same name as the options property), the name is ignored.
opts.arity
Example: 4
A number for the length to set on the returned function. If no value is provided, the length will be carried over from the sync
function's length
value.
Promise.all
-like combinator that works with an iterable of generator objects that could be passed to yield*
within a gensync generator.
Promise.race
-like combinator that works with an iterable of generator objects that could be passed to yield*
within a gensync generator.