two-promise
v0.1.3
Published
Return a promise for a callback-based function
Downloads
30
Readme
two-promise
Return a promise for a callback-based function
Example
Converting fs.readFile and fs.readdir
Read a file:
var read = toPromise(fs.readFile)
read(`${__dirname}/../package.json`)
.then(JSON.parse)
.then(pkg => t.equal(pkg.name, 'two-promise'))
Count lines from files in directory:
const dir = `${__dirname}/..`
const lsd = toPromise(fs.readdir)
const cat = toPromise(fs.readFile)
const pwd = f => `${dir}/${f}`
lsd(dir)
.then(files => files.map(pwd))
.then(paths => Promise.all(paths.map(f => cat(f).catch(resolve))))
.then(buffers => buffers.filter(b => b.length).join('').trim())
.then(concatenated => concatenated.split('\n').length)
.then(console.log)
Install
% npm install --save two-promise
const toPromise = require('two-promise')