Skip to content

Commit 4ff6c86

Browse files
committed
Add promise support
1 parent 1c9571c commit 4ff6c86

File tree

3 files changed

+109
-34
lines changed

3 files changed

+109
-34
lines changed

index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var fs = require("fs");
55
var mime = require("mime");
66
var Util = require("./util");
77
var Url = require("url");
8+
var Promise = require("./promise");
89

910
/** section: github
1011
* class Client
@@ -362,8 +363,25 @@ var Client = module.exports = function(config) {
362363
// on error, there's no need to continue.
363364
return;
364365
}
366+
if (!callback){
367+
var promise = new Promise(function(resolve,reject){
368+
var cb = function(err, obj){
369+
if (err){
370+
reject(err);
371+
} else {
372+
resolve(obj);
373+
}
374+
};
375+
api[section][funcName].call(api, msg, block, cb);
376+
});
377+
378+
} else {
379+
api[section][funcName].call(api, msg, block, callback);
380+
}
365381

366-
api[section][funcName].call(api, msg, block, callback);
382+
383+
384+
return promise;
367385
};
368386
}
369387
else {

package.json

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
{
2-
"name" : "github",
3-
"version" : "0.2.4",
4-
"description" : "NodeJS wrapper for the GitHub API",
5-
"author": "Mike de Boer <[email protected]>",
6-
"contributors": [
7-
{ "name": "Mike de Boer", "email": "[email protected]" },
8-
{ "name": "Fabian Jakobs", "email": "[email protected]" }
9-
],
10-
"homepage": "http://github.com/mikedeboer/node-github",
11-
"repository" : {
12-
"type" : "git",
13-
"url" : "http://github.com/mikedeboer/node-github.git"
2+
"name": "github",
3+
"version": "0.2.4",
4+
"description": "NodeJS wrapper for the GitHub API",
5+
"author": "Mike de Boer <[email protected]>",
6+
"contributors": [
7+
{
8+
"name": "Mike de Boer",
9+
"email": "[email protected]"
1410
},
15-
"engine" : {
16-
"node": ">=0.4.0"
17-
},
18-
"dependencies": {
19-
"mime": "^1.2.11"
20-
},
21-
"devDependencies": {
22-
"async": "^0.9.0",
23-
"oauth": "~0.9.7",
24-
"optimist": "~0.6.0",
25-
"mocha": "~1.13.0"
26-
},
27-
"main" : ".",
28-
"scripts": {
29-
"test": "node ./test/all.js"
30-
},
31-
"license": "MIT",
32-
"licenses": [{
33-
"type": "The MIT License",
34-
"url": "http://www.opensource.org/licenses/mit-license.php"
35-
}]
11+
{
12+
"name": "Fabian Jakobs",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"homepage": "http://github.com/mikedeboer/node-github",
17+
"repository": {
18+
"type": "git",
19+
"url": "http://github.com/mikedeboer/node-github.git"
20+
},
21+
"engine": {
22+
"node": ">=0.4.0"
23+
},
24+
"dependencies": {
25+
"es6-promise": "^2.2.0",
26+
"mime": "^1.2.11"
27+
},
28+
"devDependencies": {
29+
"async": "^0.9.0",
30+
"oauth": "~0.9.7",
31+
"optimist": "~0.6.0",
32+
"mocha": "~1.13.0"
33+
},
34+
"main": ".",
35+
"scripts": {
36+
"test": "node ./test/all.js"
37+
},
38+
"license": "MIT",
39+
"licenses": [
40+
{
41+
"type": "The MIT License",
42+
"url": "http://www.opensource.org/licenses/mit-license.php"
43+
}
44+
]
3645
}

promise.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*jslint browser: true, node: true, maxlen: 128 */
2+
/*global self */
3+
4+
"use strict";
5+
6+
var globalObject, hasPromiseSupport;
7+
8+
function isFunction(x) {
9+
return typeof x === "function";
10+
}
11+
12+
// Seek the global object
13+
if (global !== undefined) {
14+
globalObject = global;
15+
} else if (window !== undefined && window.document) {
16+
globalObject = window;
17+
} else {
18+
globalObject = self;
19+
}
20+
21+
hasPromiseSupport =
22+
23+
globalObject.hasOwnProperty("Promise") &&
24+
25+
// Some of these methods are missing from
26+
// Firefox/Chrome experimental implementations
27+
globalObject.Promise.hasOwnProperty("resolve") &&
28+
globalObject.Promise.hasOwnProperty("reject") &&
29+
globalObject.Promise.hasOwnProperty("all") &&
30+
globalObject.Promise.hasOwnProperty("race") &&
31+
32+
// Older version of the spec had a resolver object
33+
// as the arg rather than a function
34+
(function () {
35+
/*jslint unparam: true */
36+
var resolve, p;
37+
p = new globalObject.Promise(function (r) { resolve = r; });
38+
return isFunction(resolve);
39+
}());
40+
41+
// Export the native Promise implementation if it looks like it matches
42+
// the specificiation. Otherwise, return the es6-promise implementation
43+
// by @jaffathecake.
44+
if (hasPromiseSupport) {
45+
module.exports = globalObject.Promise;
46+
} else {
47+
module.exports = require("es6-promise").Promise;
48+
}

0 commit comments

Comments
 (0)