makepromise
![npm version](https://badge.fury.io/js/makepromise.svg)
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()
.then((ws) => {
file = ws.path
console.log(ws.path)
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,
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()
.then((ws) => {
file = ws.path
console.log(ws.path)
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) },
console.error
)
More examples
Check test/spec/integration
for the following tests:
Licence: MIT
(c) Sobesednik-Media 2017