Skip to content

Add promise support #258

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
75 changes: 42 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
{
"name" : "github",
"version" : "0.2.4",
"description" : "NodeJS wrapper for the GitHub API",
"author": "Mike de Boer <[email protected]>",
"contributors": [
{ "name": "Mike de Boer", "email": "[email protected]" },
{ "name": "Fabian Jakobs", "email": "[email protected]" }
],
"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 <[email protected]>",
"contributors": [
{
"name": "Mike de Boer",
"email": "[email protected]"
},
"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": "[email protected]"
}
],
"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"
}
]
}
48 changes: 48 additions & 0 deletions promise.js
Original file line number Diff line number Diff line change
@@ -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;
}

Choose a reason for hiding this comment

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

You do not need any of these tests. Just go ahead and use global directly. It has existed in node since the beginning. browserify sets the value of global to window anyways, so it will exist even in the browser.


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") &&

Choose a reason for hiding this comment

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

Why are you testing for all these methods on Promise? You use exactly none of them!

Drop lines 23-30.

EDIT: Drop lines 25-30


// 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;

Choose a reason for hiding this comment

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

I'd prefer bluebird. Better performance, and it's what all the cool kids are using.

}