Code Monkey home page Code Monkey logo

promisify-if-no-callback's Introduction

promisify-if-no-callback

The promisify-if-no-callback package is a utility that converts Node.js-style callback functions into Promise-based functions when no callback is provided, allowing you to work with asynchronous operations using either traditional callbacks or modern Promise-based syntax.

Installation

You can install the promisify-if-no-callback package via NPM:

npm install promisify-if-no-callback

Usage

To use the promisify-if-no-callback function, require it in your Node.js application:

const promisifyIfNoCallback = require('promisify-if-no-callback');

Syntax

const promisifiedFunction = promisifyIfNoCallback(originalFunction);

Parameters

  • originalFunction (Function): The function you want to promisify. It should follow the Node.js-style callback pattern where the last argument is a callback function (function (err, result) {}).

Return Value

The promisify-if-no-callback function returns a new function that can be used with Promises. When you call this new function, it either behaves like the original function with a callback or returns a Promise, depending on how you invoke it.

If you pass a callback as the last argument, the function will behave as the original function, allowing you to use callbacks for handling results.

If you don't pass a callback, the promisify-if-no-callback function returns a Promise that resolves with the result when the asynchronous operation completes successfully or rejects with an error if the operation encounters an error.

Examples

1. Promisify an Asynchronous Function with a Callback

const fs = require('fs');
const promisifyIfNoCallback = require('promisify-if-no-callback');

// Promisify the fs.readFile function
const readFilePromise = promisifyIfNoCallback(fs.readFile);

// Use the Promise-based function
readFilePromise('file.txt', 'utf8')
  .then(data => {
    console.log('File contents:', data);
  })
  .catch(err => {
    console.error('Error reading file:', err);
  });

2. Use the Original Callback-Style Invocation

// Assume you have an asynchronous function with a callback
function asyncFunctionWithCallback(param1, param2, callback) {
  // Some asynchronous operation
  setTimeout(() => {
    const result = param1 + param2;
    callback(null, result); // Pass null as the first argument if there's no error
  }, 1000);
}

// Use promisifyIfNoCallback to convert the function to a Promise-based function
const asyncFunctionPromise = promisifyIfNoCallback(asyncFunctionWithCallback);

// Using the original callback-style invocation
asyncFunctionWithCallback(2, 3, (err, result) => {
  if (err) {
    console.error('Error:', err);
  } else {
    console.log('Result (callback):', result);
  }
});

License

This project is licensed under the ISC License.

Issues

If you encounter any problems or have questions about the library, please feel free to open an issue on the GitHub repository.

promisify-if-no-callback's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.