Gathering detailed insights and metrics for gensync
Gathering detailed insights and metrics for gensync
Gathering detailed insights and metrics for gensync
Gathering detailed insights and metrics for gensync
Allows users to use generators in order to write common functions that can be both sync or async.
npm install gensync
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
91 Stars
19 Commits
13 Forks
3 Watchers
12 Branches
3 Contributors
Updated on May 27, 2025
Latest Version
1.0.0-beta.2
Package Id
gensync@1.0.0-beta.2
Size
7.27 kB
NPM Version
6.14.5
Node Version
14.5.0
Published on
Oct 27, 2020
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
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.
1const fs = require("fs"); 2const gensync = require("gensync"); 3 4const readFile = gensync({ 5 sync: fs.readFileSync, 6 errback: fs.readFile, 7}); 8 9const myOperation = gensync(function* (filename) { 10 const code = yield* readFile(filename, "utf8"); 11 12 return "// some custom prefix\n" + code; 13}); 14 15// Load and add the prefix synchronously: 16const result = myOperation.sync("./some-file.js"); 17 18// Load and add the prefix asynchronously with promises: 19myOperation.async("./some-file.js").then(result => { 20 21}); 22 23// Load and add the prefix asynchronously with promises: 24myOperation.errback("./some-file.js", (err, result) => { 25 26});
This could even be exposed as your official API by doing
1// Using the common 'Sync' suffix for sync functions, and 'Async' suffix for 2// promise-returning versions. 3exports.myOperationSync = myOperation.sync; 4exports.myOperationAsync = myOperation.async; 5exports.myOperation = myOperation.errback;
or potentially expose one of the async versions as the default, with a
.sync
property on the function to expose the synchronous version.
1module.exports = myOperation.errback; 2module.exports.sync = myOperation.sync;
Returns a function that can be "await"-ed in another gensync
generator
function, or executed via
.sync(...args)
- Returns the computed value, or throws..async(...args)
- Returns a promise for the computed value..errback(...args, (err, result) => {})
- Calls the callback with the computed value, or error.Wraps the generator to populate the .sync
/.async
/.errback
helpers above to
allow for evaluation of the generator for the final value.
1const readFile = function* () { 2 return 42; 3}; 4 5const readFileAndMore = gensync(function* (){ 6 const val = yield* readFile(); 7 return 42 + val; 8}); 9 10// In general cases 11const code = readFileAndMore.sync("./file.js", "utf8"); 12readFileAndMore.async("./file.js", "utf8").then(code => {}) 13readFileAndMore.errback("./file.js", "utf8", (err, code) => {}); 14 15// In a generator being called indirectly with .sync/.async/.errback 16const code = yield* readFileAndMore("./file.js", "utf8");
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.
1const readFile = gensync({ 2 sync: fs.readFileSync, 3 errback: fs.readFile, 4}); 5 6const code = readFile.sync("./file.js", "utf8"); 7readFile.async("./file.js", "utf8").then(code => {}) 8readFile.errback("./file.js", "utf8", (err, code) => {});
Promise.all
-like combinator that works with an iterable of generator objects
that could be passed to yield*
within a gensync generator.
1const loadFiles = gensync(function* () { 2 return yield* gensync.all([ 3 readFile("./one.js"), 4 readFile("./two.js"), 5 readFile("./three.js"), 6 ]); 7});
Promise.race
-like combinator that works with an iterable of generator objects
that could be passed to yield*
within a gensync generator.
1const loadFiles = gensync(function* () { 2 return yield* gensync.race([ 3 readFile("./one.js"), 4 readFile("./two.js"), 5 readFile("./three.js"), 6 ]); 7});
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/14 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
69 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More