promise-back

Augment a thunk such that it returns a promise if applied without a callback.

Usage no npm install needed!

<script type="module">
  import promiseBack from 'https://cdn.skypack.dev/promise-back';
</script>

README

promise-back

Augment a thunk such that it returns a promise if applied without a callback.

Build Status Code Climate js-standard-style

npm install promise-back --save

You can also use Duo, Bower or download the files manually.

npm stats

npm NPM downloads Dependency Status

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 to promiseBack without a wrapper; You may also use Function.prototype.bind to 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, returns undefined, otherwise, returns a Promise.

Contributing

SEE: contributing.md

Licenses

GitHub license