Make a JavaScript function support both promise and callback styles.
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.
npm install polygoat
var pg = require('polygoat');
or
<script src="node_modules/polygoat/index.js"></script>
var pg = window.polygoat
// 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
See example.js
npm install standard
npm test
Goat icon by Agne Alesiute from the Noun Project