0.4.1 • Published 10 years ago
promise-back v0.4.1
promise-back
Augment a thunk such that it returns a promise if applied without a callback.
npm install promise-back --saveYou can also use Duo, Bower or download the files manually.
npm stats
API Example
require
var promiseBack = require('promise-back')expose dual interface async function
var read = require('fs').readFile
var path = require('path')
var filepath = path.resolve(__dirname, 'package.json')
var encoding = 'utf8'
var readable = promiseBack(read.bind(null, filepath, encoding))NOTE: if you already have a function that takes a callback as it's only parameter, you can just pass it to
promiseBack. Modules that expose such an interface can be passed directly topromiseBackwithout a wrapper; You may also useFunction.prototype.bindto achieve the same.
use callback interface
readable(function (_, str) {
console.log(JSON.parse(pkg).description)
})
//=> Augment a thunk such that it returns a promise if applied without a callback.use promise interface
readable()
.then(JSON.parse)
.then(selectn('description'))
.then(console.log)
.catch(console.error)
//=> Augment a thunk such that it returns a promise if applied without a callback.API
promiseBack(fn)
arguments
fn: (Function)Thunk.
returns
(Function)Returns a function that when applied with a callback, returnsundefined, otherwise, returns aPromise.
Contributing
SEE: contributing.md