New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

makepromise

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

makepromise

Make a native Promise from a function with a callback

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.5K
decreased by-2.33%
Maintainers
1
Weekly downloads
 
Created
Source

makepromise

npm version

Make native Promise from function with callback

makePromise(fn:Function(...args, cb:Function(err:Error, res:any))) => Promise<any>

Create a promise from a function which accepts callback as the last argument, and where callback will be called with (error, result).

For example, you can unlink a closed file:

'use strict'

const fs = require('fs')
const wrote = require('wrote')
const makePromise = require('makepromise')

let file
wrote() // create a temp file and open a writable stream
    .then((ws) => {
        file = ws.path
        console.log(ws.path) // /var/folders/s0/tmp/T/wrote-69822.data
        return new Promise((resolve, reject) => {
            ws.once('close', resolve)
            ws.once('error', reject)
            ws.close()
        })
    })
    .then(() => {
        const promise = makePromise(fs.unlink, file)
        return promise
    })
    .then(
        console.log, // undefined
        console.error
    )

makePromise(fn:Function(...args, cb:Function(err:Error, res:any), resolveValue:any)) => Promise<any>

Pass resolveValue as second argument to make promise be resolved with it.

'use strict'

const fs = require('fs')
const wrote = require('wrote')
const makePromise = require('makepromise')

let file
wrote() // create a temp file and open a writable stream
    .then((ws) => {
        file = ws.path
        console.log(ws.path) // /var/folders/s0/tmp/T/wrote-69822.data
        return new Promise((resolve, reject) => {
            ws.once('close', resolve)
            ws.once('error', reject)
            ws.close()
        })
    })
    .then(() => {
        const promise = makePromise(fs.unlink, file, file)
        return promise
    })
    .then(
        (res) => { console.log(res === file) }, // true
        console.error
    )

More examples

Check test/spec/integration for the following tests:


Licence: MIT

(c) Sobesednik-Media 2017

Keywords

FAQs

Package last updated on 20 May 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc