polygoat

Make JavaScript functions that support both promise and callback styles.


Keywords
async, promise, callback, hybrid, hybridify, promisify, thenify
License
ISC
Install
npm install polygoat@1.1.4

Documentation

polygoat

Build Status js-standard-style

logo

polygoat is a tool to make functions support both callback and promise styles.

  • very small, < 30 lines of code
  • no promise support/polyfill required
  • simple, hack-free
  • Node.js and browsers
  • fast, see benchmark

Getting started

npm install polygoat


var pg = require('polygoat');

or

<script src="node_modules/polygoat/index.js"></script>
var pg = window.polygoat

Usage

// wrap an asynchronous function with polygoat
function hybridReaddir (path, callback) {
  return pg(function (done) {
    fs.readdir(path, done)
  }, callback)
}

// hybridReaddir can be used as a promise
hybridReaddir('/').then(console.log)

// or with a callback
hybridReaddir('/', console.log)

// or with async/await via Babel
// https://ponyfoo.com/articles/understanding-javascript-async-await
async () => {
  console.log('listing...')
  console.log(await hybridReaddir('/'))
  console.log('done')
}()

// you can also pass the Promise implementation of your choice
var bluebird = require('bluebird')

function hybridReaddir (path, callback) {
  return pg(function (done) {
    fs.readdir(path, done)
  }, callback, bluebird)
}

hybridReaddir() instanceof bluebird // true

Example

See example.js

Benchmark

See benchmark

Test

npm install standard
npm test

Goat icon by Agne Alesiute from the Noun Project