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
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:
- How to unlock a file with
lockfile
and close write stream - How to write to a file stream open with
fs.createWriteStream()
and end the stream - How to unlink a file with
fs.unlink
- How to read stat with
fs.stat
- How to catch errors (example with trying to call
fs.stat
with non-existent file)
Licence: MIT
(c) Sobesednik-Media 2017