Socket
Socket
Sign inDemoInstall

@gar/promisify

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gar/promisify

Promisify an entire class or object


Version published
Maintainers
1
Weekly downloads
7,817,683
decreased by-7.7%

Weekly downloads

Package description

What is @gar/promisify?

The @gar/promisify npm package is designed to convert callback-based functions into Promises. This is particularly useful when working with older Node.js or JavaScript libraries that do not natively support Promises, allowing developers to write cleaner, more modern asynchronous code using async/await syntax or .then() chains.

What are @gar/promisify's main functionalities?

Promisifying a single function

Converts a single callback-based function (in this example, fs.readFile) into a function that returns a Promise. This allows for the use of .then() and .catch() for handling asynchronous operations.

const promisify = require('@gar/promisify');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);

readFileAsync('example.txt', 'utf8').then(contents => {
  console.log(contents);
}).catch(error => {
  console.error(error);
});

Promisifying an object's methods

Converts all callback-based functions within an object (in this example, all methods of the 'fs' module) into functions that return Promises. This simplifies working with modules that have multiple asynchronous methods.

const promisify = require('@gar/promisify');
const fs = promisify.all(require('fs'));

fs.readFile('example.txt', 'utf8').then(contents => {
  console.log(contents);
}).catch(error => {
  console.error(error);
});

Other packages similar to @gar/promisify

Readme

Source

@gar/promisify

Promisify an entire object or class instance

This module leverages es6 Proxy and Reflect to promisify every function in an object or class instance.

It assumes the callback that the function is expecting is the last parameter, and that it is an error-first callback with only one value, i.e. (err, value) => .... This mirrors node's util.promisify method.

In order that you can use it as a one-stop-shop for all your promisify needs, you can also pass it a function. That function will be promisified as normal using node's built-in util.promisify method.

node's custom promisified functions will also be mirrored, further allowing this to be a drop-in replacement for the built-in util.promisify.

Examples

Promisify an entire object


const promisify = require('@gar/promisify')

class Foo {
  constructor (attr) {
    this.attr = attr
  }

  double (input, cb) {
    cb(null, input * 2)
  }

const foo = new Foo('baz')
const promisified = promisify(foo)

console.log(promisified.attr)
console.log(await promisified.double(1024))

Promisify a function


const promisify = require('@gar/promisify')

function foo (a, cb) {
  if (a !== 'bad') {
    return cb(null, 'ok')
  }
  return cb('not ok')
}

const promisified = promisify(foo)

// This will resolve to 'ok'
promisified('good')

// this will reject
promisified('bad')

Keywords

FAQs

Last updated on 16 Feb 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