Skip to content

Make a JavaScript function support both promise and callback styles.

License

Notifications You must be signed in to change notification settings

johnzeringue/polygoat

 
 

Repository files navigation

polygoat

logo

Make a JavaScript function support both promise and callback styles.

Build Status js-standard-style

This is great if you want to offer your users the choice between the two. It is also an elegant way to support older platforms without Promise support and let the users decide if they want to add a Promise polyfill.

Getting started

npm install polygoat


var pg = require('polygoat');

or

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

Usage

// wrap and expose your asynchronous function with polygoat
function myFunction (some, arg, callback) {
  pg(function (done) {
    doSomethingAsync(some, arg, done)
  }, callback)
}

// myFunction can now be used with promise style
myFunction('foo', 'bar').then(console.log)

// or callback style
myFunction('foo', 'bar', console.log)

// you can also pass the Promise implementation you wish polygoat to use
var bluebird = require('bluebird')

function myFunction (callback) {
  pg(function (done) {
    doSomethingAsync(done)
  }, callback, bluebird)
}

myFunction() instanceof bluebird // true

Example

See example.js

Test

npm install standard
npm test

Goat icon by Agne Alesiute from the Noun Project

About

Make a JavaScript function support both promise and callback styles.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%