diff --git a/index.js b/index.js index 0c062efcf8..b3ee6b7f67 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 @@ -362,8 +363,25 @@ var Client = module.exports = function(config) { // on error, there's no need to continue. return; } + if (!callback){ + var promise = new 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 { + api[section][funcName].call(api, msg, block, callback); + } - api[section][funcName].call(api, msg, block, callback); + + + return promise; }; } else { diff --git a/package.json b/package.json index ec46d02972..f8cbd04f04 100644 --- a/package.json +++ b/package.json @@ -1,36 +1,45 @@ { - "name" : "github", - "version" : "0.2.4", - "description" : "NodeJS wrapper for the GitHub API", - "author": "Mike de Boer ", - "contributors": [ - { "name": "Mike de Boer", "email": "info@mikedeboer.nl" }, - { "name": "Fabian Jakobs", "email": "fabian@c9.io" } - ], - "homepage": "http://github.com/mikedeboer/node-github", - "repository" : { - "type" : "git", - "url" : "http://github.com/mikedeboer/node-github.git" + "name": "github", + "version": "0.2.4", + "description": "NodeJS wrapper for the GitHub API", + "author": "Mike de Boer ", + "contributors": [ + { + "name": "Mike de Boer", + "email": "info@mikedeboer.nl" }, - "engine" : { - "node": ">=0.4.0" - }, - "dependencies": { - "mime": "^1.2.11" - }, - "devDependencies": { - "async": "^0.9.0", - "oauth": "~0.9.7", - "optimist": "~0.6.0", - "mocha": "~1.13.0" - }, - "main" : ".", - "scripts": { - "test": "node ./test/all.js" - }, - "license": "MIT", - "licenses": [{ - "type": "The MIT License", - "url": "http://www.opensource.org/licenses/mit-license.php" - }] + { + "name": "Fabian Jakobs", + "email": "fabian@c9.io" + } + ], + "homepage": "http://github.com/mikedeboer/node-github", + "repository": { + "type": "git", + "url": "http://github.com/mikedeboer/node-github.git" + }, + "engine": { + "node": ">=0.4.0" + }, + "dependencies": { + "es6-promise": "^2.2.0", + "mime": "^1.2.11" + }, + "devDependencies": { + "async": "^0.9.0", + "oauth": "~0.9.7", + "optimist": "~0.6.0", + "mocha": "~1.13.0" + }, + "main": ".", + "scripts": { + "test": "node ./test/all.js" + }, + "license": "MIT", + "licenses": [ + { + "type": "The MIT License", + "url": "http://www.opensource.org/licenses/mit-license.php" + } + ] } diff --git a/promise.js b/promise.js new file mode 100644 index 0000000000..75fdfd7318 --- /dev/null +++ b/promise.js @@ -0,0 +1,48 @@ +/*jslint browser: true, node: true, maxlen: 128 */ +/*global self */ + +"use strict"; + +var globalObject, hasPromiseSupport; + +function isFunction(x) { + return typeof x === "function"; +} + +// Seek the global object +if (global !== undefined) { + globalObject = global; +} else if (window !== undefined && window.document) { + globalObject = window; +} else { + globalObject = self; +} + +hasPromiseSupport = + + globalObject.hasOwnProperty("Promise") && + + // Some of these methods are missing from + // Firefox/Chrome experimental implementations + globalObject.Promise.hasOwnProperty("resolve") && + globalObject.Promise.hasOwnProperty("reject") && + globalObject.Promise.hasOwnProperty("all") && + globalObject.Promise.hasOwnProperty("race") && + + // Older version of the spec had a resolver object + // as the arg rather than a function + (function () { + /*jslint unparam: true */ + var resolve, p; + p = new globalObject.Promise(function (r) { resolve = r; }); + return isFunction(resolve); + }()); + +// Export the native Promise implementation if it looks like it matches +// the specificiation. Otherwise, return the es6-promise implementation +// by @jaffathecake. +if (hasPromiseSupport) { + module.exports = globalObject.Promise; +} else { + module.exports = require("es6-promise").Promise; +} \ No newline at end of file