JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4083
  • Score
    100M100P100Q116030F
  • License MIT

Make a native Promise from a function with a callback

Package Exports

  • makepromise
  • makepromise/es5/src

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (makepromise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

makepromise

npm version

Make native Promise from a function with a callback.

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):

import { unlink } from 'fs'
import { createWritable } from 'wrote'
import makePromise from '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:

import { unlink } from 'fs'
import makePromise from '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.

import { unlink } from 'fs'
import { createWritable } from 'wrote'
import makePromise from '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:

ES5

DO NOT USE THIS. USE NEWER NODE.JS 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')

Licence: MIT

(c) Art Deco Code 2018