Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goatify #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

var promisified = bluebird.promisify(callbackFun)

var goatified = pg.goatify(callbackFun)

var callSuite = new benchmark.Suite('call time')
callSuite
.add('polygoat promise', function () {
Expand All @@ -54,6 +56,9 @@
.add('bluebird promisified (eval on Node.js, closure on browser)', function () {
promisified().then(function () {})
})
.add('polygoat goatified', function () {
goatified().then(function () {})
})
.on('cycle', function (event) {
console.log(String(event.target))
})
Expand Down Expand Up @@ -84,6 +89,11 @@
notAsync(cb)
})
})
.add('polygoat goatify', function () {
return pg.goatify(function (cb) {
notAsync(cb)
})
})
.on('cycle', function (event) {
console.log(String(event.target))
})
Expand Down
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@
}
}

function goatify (fn) {
return function () {
var args = Array.prototype.slice.call(arguments)
var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null
return polygoat(function (done) {
args.push(done)
fn.apply(null, args)
}, callback)
}
}

function goatifyAll (obj) {
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
var v = obj[k]
if (typeof v === 'function') { // FIXME IIRC this fails on old browsers

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what old browser would this fail in? This works at least back to IE6.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this would fail on previous major version of phantomjs (don't know what JavaScriptCore version it used though)

obj[k] = goatify(v)
}
}
}
return obj
}

polygoat.goatify = goatify
polygoat.goatifyAll = goatifyAll

if (typeof module !== 'undefined' && module.exports) {
module.exports = polygoat
} else {
Expand Down