Huge News!Announcing our $40M Series B led by Abstract Ventures.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.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3K
increased by6.12%
Maintainers
1
Weekly downloads
 
Created
Source

makepromise

npm version

Make native Promise from function with callback.

ES5

The package uses some newer language features. For your convenience, it's been transpiled to be compatible with Node 4. You can use the following snippet.

const makePromise = require('makepromise/es5/src/')

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

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

For example, you can unlink a file (here, a temp file is created with wrote package):

const { unlink } = require('fs')
const { createWritable } = require('wrote')
const makePromise = require('makepromise')

let file
(async () => {
    // create a temp file and open a writable stream
    const ws = await createWritable()

    const { path: file } = ws // /var/folders/s0/tmp/T/wrote-69822.data

    // here, just close the stream without makepromise, because it's a different
    // kind of constructor, ie. resolve-reject and not error callback handlers
    await new Promise((resolve, reject) => {
        ws.once('close', resolve)
        ws.once('error', reject)
        ws.close()
    })
    await makePromise(unlink, file)
})()

In addition, errors will be updated to include the stack trace of when makePromise was called, rather than have node internal error stack, or no stack at all:

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

(async () => {
    try {
        await makePromise(fs.unlink, 'error-test-file')
    } catch ({ stack }){
        console.log(stack)
        /*
        Error: ENOENT: no such file or directory, unlink 'error-test-file'
            at cb (makepromise/src/index.js:28:31)
            at makePromise (makepromise/src/index.js:18:16)
            at Object.<anonymous> (makepromise/examples/error-stack.js:4:1)
        */

        // without this feature:
        /*
        Error: ENOENT: no such file or directory, unlink 'error-test-file'
        */
    }
})()

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.

const { unlink } = require('fs')
const { createWritable } = require('wrote')
const makePromise = require('makepromise');

(async () => {
    // create a temp file and open a writable stream
    const ws = await createWritable()
    await closeWritable(ws)

    const { path: file } = ws

    console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data

    // pass 3rd argument as the return value
    const resolvedFile = await makePromise(unlink, file, file)
    console.log(resolvedFile === file) // true
})()

More examples

Check test/spec/integration for the following tests:


Licence: MIT

(c) Sobesednik-Media 2017

Keywords

FAQs

Package last updated on 29 Dec 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