polygoat

Make JavaScript functions that support both promise and callback styles.

Usage no npm install needed!

<script type="module">
  import polygoat from 'https://cdn.skypack.dev/polygoat';
</script>

README

polygoat

Build Status js-standard-style

logo

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

  • 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 hybridFunction (path, callback) {
  return pg(function (done) {
    fs.readdir(path, done)
  }, callback)
}

// hybridFunction can be used with promise style
hybridFunction('/').then(console.log)

// or callback style
hybridFunction('/', console.log)

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

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

hybridFunction() 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