Skip to content

Instantly share code, notes, and snippets.

@NotIntMan
Last active November 7, 2015 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotIntMan/bfd92286a50594a80c0d to your computer and use it in GitHub Desktop.
Save NotIntMan/bfd92286a50594a80c0d to your computer and use it in GitHub Desktop.
Little module to let callBack'ed functions return a Promise.
'use strict'
function denodeifySpecial (func, callBack) {
return function () {
let _this = this
let len = arguments.length
let args = new Array(len+1)
for (let i = 0; i<len; i++)
args[i] = arguments[i]
return new Promise((resolve, reject)=> {
args[len] = function() {
try {
resolve(callBack.apply(this, arguments))
} catch(e) {
reject(e)
}
}
func.apply(_this, args)
})
}
}
function standardCallBack (err, data) {
if (err)
throw err
else
return data
}
function denodeify (func) {
return denodeifySpecial(func, standardCallBack)
}
module.exports = denodeify
module.exports.special = denodeifySpecial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment