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 Promise from a function with a callback and preserve its error stack.

  • 3.0.0
  • Source
  • npm
  • Socket score

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

makepromise

npm version

makepromise can be used to get a Promise from a function with a callback. It will also make sure that the error stack starts at the line where the makepromise was called.

yarn add -E makepromise

Table Of Contents

API

The package exports a default makepromise function.

import makePromise from 'makepromise'

async makePromise(
  fn: function(...args, cb),
  args: (*[]|*),
): void

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 and result. The arguments must be passed either as an array, or a single value.

The example below shows how to use makePromise with an array of arguments (promisified truncate) and a single argument (promisified unlink). The context of the example is 2 methods from a lib to create a temp file, and read data from a file.

import { truncate, unlink, existsSync } from 'fs'
import makePromise from 'makepromise'
import { createTempFile, readFile } from './lib'

(async () => {
  try {
    // 0. SETUP: create a temp file.
    const path = await createTempFile('hello-world')
    const data = readFile(path)
    console.log('Created temp file %s', path)
    console.log('Exists: %s', existsSync(path))
    console.log('Content: "%s"', data)

    // 1. TRUNCATE to 5 characters.
    await makePromise(truncate, [path, 5])
    const data2 = await readFile(path)
    console.log('Content: "%s"', data2)

    // 2. ERASE the temp file.
    await makePromise(unlink, path)
    console.log('Exists: %s', existsSync(path))
  } catch (err) {
    console.log(err)
  }
})()
Created temp file example/temp.data
Exists: true
Content: "hello-world"
Content: "hello"
Exists: false

async makePromise(
  fn: function(...args, cb),
  args: (*[]|*),
  resolveValue: *,
): void

When resolveValue is passed as the last argument to the makePromise function, the returned promise will be forced to resolve with it.

import { unlink } from 'fs'
import makePromise from 'makepromise'
import { createTempFile } from './lib'

(async () => {
  try {
    // 0. SETUP: create a temp file.
    const path = await createTempFile()

    // 1. UNLINK and return the path to the temp file.
    const erasedPath = await makePromise(unlink, path, path)
    console.log('Erased: %s', erasedPath)
  } catch (err) {
    console.log(err)
  }
})()
Erased: example/temp.data

Binding Methods

Sometimes, it is important to bind methods of instances to their contexts, otherwise they will loose access to this.

For example. when closing a Writable stream, its close method must be bound to its instance.

import { createWriteStream, unlink } from 'fs'
import makePromise from 'makepromise'
import { createTempFile, readFile } from './lib'

(async () => {
  try {
    // 0. SETUP: create a temp file.
    const path = await createTempFile()

    // 1. CREATE a write stream, and end it with data.
    const ws = createWriteStream(path)
    await makePromise(ws.end.bind(ws), 'example-data')

    // 2. CHECK that data has been written.
    const data = await readFile(path)
    console.log('Read file: "%s"', data)

    // 3. TEAR-DOWN: remove file.
    await makePromise(unlink, path)
  } catch (err) {
    console.log(err)
  }
})()
Read file: "example-data"

Error Stack

This modules will make sure that errors are updated to include the stack trace of when makePromise was called, rather than have the Node's internal error stack or no stack at all.

import { unlink } from 'fs'
import makePromise from 'makepromise'

(async () => {
  try {
    await makePromise(unlink, 'error-test-file')
  } catch ({ stack }) {
    console.log(stack)
  }
})()
Error: ENOENT: no such file or directory, unlink 'error-test-file'
    at /Users/zavr/adc/makepromise/example/error-stack.js:6:11
    at Object.<anonymous> (/Users/zavr/adc/makepromise/example/error-stack.js:10:3)
    at Module._compile (/Users/zavr/adc/makepromise/node_modules/pirates/lib/index.js:83:24)
    at Object.newLoader [as .js] (/Users/zavr/adc/makepromise/node_modules/pirates/lib/index.js:88:7)

Without this functionality, the error stack would not appear.

import { unlink } from 'fs'

(async () => {
  try {
    await new Promise((r, j) => {
      unlink('error-test-file', (err) => {
        if (err) return j(err)
        return r()
      })
    })
  } catch ({ stack }) {
    console.log(stack)
  }
})()
Error: ENOENT: no such file or directory, unlink 'error-test-file'

TODO

  • fix missing v2.0.0 commit.

(c) Art Deco 2018

Keywords

FAQs

Package last updated on 31 Aug 2018

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