🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@braintree/wrap-promise

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@braintree/wrap-promise

A small lib to return promises or use callbacks

3.0.2
latest
Source
npm
Version published
Maintainers
0
Created

What is @braintree/wrap-promise?

@braintree/wrap-promise is a utility library that helps in converting callback-based functions into promise-based ones. This is particularly useful for modernizing codebases and making asynchronous code easier to read and maintain.

What are @braintree/wrap-promise's main functionalities?

Wrap a callback-based function

This feature allows you to wrap a traditional callback-based function and convert it into a promise-based function. This makes it easier to work with asynchronous code using modern JavaScript syntax.

const wrapPromise = require('@braintree/wrap-promise');

function callbackFunction(arg, callback) {
  setTimeout(() => {
    if (arg) {
      callback(null, 'Success');
    } else {
      callback('Error');
    }
  }, 1000);
}

const promiseFunction = wrapPromise(callbackFunction);

promiseFunction(true)
  .then(result => console.log(result))
  .catch(error => console.error(error));

Wrap a function with multiple arguments

This feature demonstrates how to wrap a function that takes multiple arguments before the callback. The wrapped function can then be used as a promise-based function.

const wrapPromise = require('@braintree/wrap-promise');

function multiArgFunction(arg1, arg2, callback) {
  setTimeout(() => {
    if (arg1 && arg2) {
      callback(null, 'Success');
    } else {
      callback('Error');
    }
  }, 1000);
}

const promiseFunction = wrapPromise(multiArgFunction);

promiseFunction('arg1', 'arg2')
  .then(result => console.log(result))
  .catch(error => console.error(error));

Other packages similar to @braintree/wrap-promise

FAQs

Package last updated on 20 Dec 2024

Did you know?

Socket

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