JSPM

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

Make a native Promise from a function with a callback

Package Exports

  • makepromise

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