Last active
November 7, 2015 07:58
-
-
Save NotIntMan/bfd92286a50594a80c0d to your computer and use it in GitHub Desktop.
Little module to let callBack'ed functions return a Promise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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