diff --git a/index.js b/index.js index 0c062efcf8..af03b46259 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ var fs = require("fs"); var mime = require("mime"); var Util = require("./util"); var Url = require("url"); +var Promise = require("./promise"); /** section: github * class Client @@ -178,6 +179,7 @@ var Client = module.exports = function(config) { config.headers = config.headers || {}; this.config = config; this.debug = Util.isTrue(config.debug); + this.Promise = config.Promise || config.promise || Promise; this.version = config.version; var cls = require("./api/v" + this.version); @@ -362,8 +364,25 @@ var Client = module.exports = function(config) { // on error, there's no need to continue. return; } + if (!callback){ + if (self.Promise) { + return new self.Promise(function(resolve,reject){ + var cb = function(err, obj){ + if (err){ + reject(err); + } else { + resolve(obj); + } + }; + api[section][funcName].call(api, msg, block, cb); + }); + } else { + throw new Error('neither a callback or global promise implementation was provided'); + } + } else { + api[section][funcName].call(api, msg, block, callback); + } - api[section][funcName].call(api, msg, block, callback); }; } else { diff --git a/promise.js b/promise.js new file mode 100644 index 0000000000..cd0dc60ad7 --- /dev/null +++ b/promise.js @@ -0,0 +1,17 @@ +"use strict"; + +var Promise = global.Promise || null; + +if (isFunction(Promise)) { + new Promise(function(resolver) { + if (!isFunction(resolver)) { + Promise = null; + } + }); +} + +module.exports = Promise; + +function isFunction(x) { + return typeof x === "function"; +}