Socket
Socket
Sign inDemoInstall

async-compat

Package Overview
Dependencies
2
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    async-compat

Compatibility functions for writing libraries that support synchronous, callback and promise signatures


Version published
Weekly downloads
341
increased by610.42%
Maintainers
1
Install size
15.7 kB
Created
Weekly downloads
 

Readme

Source

async-compat

Compatibility functions for writing libraries that support synchronous, callback and promise signatures.

It handles both the call signature differences between callback and promises APIs (eg. missing the last callback parameter) by converting them to callbacks. It also resolves returned parameters when promises are returned.

var compat = require('async-compat');
var assert = require(assert');

/////////////////
// synchronous
/////////////////
function fn(value1) {
  assert.equal(value1, 1);
  return 4;
}

compat.asyncFunction(fn, false /* no callbacks */, 1, function (err, result) {
  assert.ok(!err);
  assert.equal(result, 4);
});

function errorFn(value1) {
  assert.equal(value1, 1);
  return new Error('Failed');
}

compat.asyncFunction(errorFn, false /* no callbacks */, 1, function (err, result) {
  assert.ok(!!err);
});

/////////////////
// callback
/////////////////
function callbackFn(value1, callback) {
  assert.equal(value1, 1);
  callback(null, 4);
}

compat.asyncFunction(callbackFn, true /*  no callbacks */, 1, function (err, result) {
  assert.ok(!err);
  assert.equal(result, 4);
});

function errorCallbackFn(value1, callback) {
  assert.equal(value1, 1);
  callback(new Error('Failed'));
}

compat.asyncFunction(errorCallbackFn, true /*  no callbacks */, 1, function (err, result) {
  assert.ok(!!err);
});

/////////////////
// promise
/////////////////
function promiseFn(value1) {
  assert.equal(value1, 1);
  return Promise.resolve(4);
}

compat.asyncFunction(promiseFn, false /* no callbacks */, 1, function (err, result) {
  assert.ok(!err);
  assert.equal(result, 4);
});

function errorPromiseFn(value1) {
  assert.equal(value1, 1);
  return Promise.reject(new Error('Failed'));
}

compat.errorPromiseFn(promiseFn, false /* no callbacks */, 1, function (err, result) {
  assert.ok(!!err);
});

Keywords

FAQs

Last updated on 21 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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