Socket
Socket
Sign inDemoInstall

wrap-fn

Package Overview
Dependencies
75
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 0.1.0

2

component.json
{
"name": "wrap-fn",
"version": "0.0.2",
"version": "0.1.0",
"description": "support sync, async, and generator functions",

@@ -5,0 +5,0 @@ "dependencies": {

0.1.0 / 2014-09-17
==================
* Promises, synchronous errors and tests
0.0.2 / 2014-08-31

@@ -3,0 +8,0 @@ ==================

@@ -32,14 +32,44 @@ /**

// done
if (!fn) {
// done
return done.apply(ctx, [null].concat(args));
} else if (fn.length > args.length) {
// async
fn.apply(ctx, args.concat(done));
} else if (generator(fn)) {
// generator
co(fn).apply(ctx, args.concat(done));
}
// async
if (fn.length > args.length) {
return fn.apply(ctx, args.concat(done));
}
// generator
if (generator(fn)) {
return co(fn).apply(ctx, args.concat(done));
}
// sync
return sync(fn, done).apply(ctx, args);
}
}
/**
* Wrap a synchronous function execution.
*
* @param {Function} fn
* @param {Function} done
* @return {Function}
* @api private
*/
function sync(fn, done) {
return function () {
var ret;
try {
ret = fn.apply(this, arguments);
} catch (err) {
return done(err);
}
if (promise(ret)) {
ret.then(function (value) { done(null, value); }, done);
} else {
// sync
var ret = fn.apply(ctx, args);
ret instanceof Error ? done(ret) : done(null, ret);

@@ -63,1 +93,14 @@ }

}
/**
* Is `value` a promise?
*
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function promise(value) {
return value && 'function' == typeof value.then;
}
{
"name": "wrap-fn",
"version": "0.0.2",
"version": "0.1.0",
"description": "support sync, async, and generator functions",
"scripts": {
"test": "make test"
},
"keywords": [

@@ -6,0 +9,0 @@ "browser",

# wrap-fn
Low-level wrapper to support sync, async, and generator functions.
Low-level wrapper to support sync, async, promises, and generator functions.

@@ -29,3 +29,3 @@ ## Installation

function next(err) {
Called after
// Called after
}

@@ -40,3 +40,3 @@

Wrap `fn` to support sync, async and generator functions. Call `done` when finished.
Wrap `fn` to support sync, async, promises and generator functions. Call `done` when finished.

@@ -43,0 +43,0 @@ `wrap` returns a function which you can pass arguments to or set the context.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc