Coder Social home page Coder Social logo

robojones / smart-promisify Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 74 KB

A smart implementation of promisify using native promises for Node.js

Home Page: https://www.npmjs.com/package/smart-promisify

License: MIT License

JavaScript 100.00%
promisify native-promises callback promise npm nodejs javascript

smart-promisify's Introduction

smart-promisify

A smart implementation of promisify using native promises

Build Status Test Coverage

bitHound Code bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies License: MIT

Install

npm i smart-promisify

Features

With smart-promisify you can obviously wrap asynchronous functions so they return a promise.

But why smart?

The smart thing about this promisify module is, that you can use the wrapped function just like before. If you provide a callback, no promise will be returned. If you don't provide a callback, you will get your promise.

You can also change the this object of the wrapped function. If you use .apply, .call or .bind on the wrapper, the this object will also be applied to the wrapped function.

Example - Promisify fs.mkdir

const fs = require('fs')
const promisify = require('smart-promisify')

// create an async function so we can await stuff
async function example () {

  // wrap the fs.mkdir method
  let mkdir = promisify(fs.mkdir)

  // await the promise returned by the wrapper
  await mkdir('folder1')

  // you can still provide a callback.
  // the wrapper will return the return value of the fs.mkdir instead of a promise
  mkdir('folder2', console.log)

  // changing the this of the promisified function
  mkdir.call(fs, 'folder3')
  // fs.mkdir will now have fs as its this object

  // alternatively you can call the promisify function with a second argument.
  mkdir = promisify(fs.mkdir, fs)
  // the second argument will be used as this object for the wrapped function, if it isn't changed afterwards by with call, bind or apply
}

example().catch(console.error)

smart-promisify's People

Contributors

dependabot[bot] avatar robojones avatar

Stargazers

 avatar

Watchers

 avatar  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.