Skip to content

Commit 49fa098

Browse files
committed
simplify promise support
this simplifies the initial implementation of Promise support by @gero3, and allows for pluggable promise implementations. If a global `Promise` function exists, it will use that. Optionally, users can supply a promise implentation via `config.Promise`. this keeps the dependency list small, and gives people the freedom to choose their favorite Promise library.
1 parent 4ff6c86 commit 49fa098

File tree

3 files changed

+59
-98
lines changed

3 files changed

+59
-98
lines changed

index.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ var Client = module.exports = function(config) {
179179
config.headers = config.headers || {};
180180
this.config = config;
181181
this.debug = Util.isTrue(config.debug);
182+
this.Promise = config.Promise || config.promise || Promise;
182183

183184
this.version = config.version;
184185
var cls = require("./api/v" + this.version);
@@ -364,24 +365,24 @@ var Client = module.exports = function(config) {
364365
return;
365366
}
366367
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-
368+
if (self.Promise) {
369+
return new self.Promise(function(resolve,reject){
370+
var cb = function(err, obj){
371+
if (err){
372+
reject(err);
373+
} else {
374+
resolve(obj);
375+
}
376+
};
377+
api[section][funcName].call(api, msg, block, cb);
378+
});
379+
} else {
380+
throw new Error('neither a callback or global promise implementation was provided');
381+
}
378382
} else {
379383
api[section][funcName].call(api, msg, block, callback);
380384
}
381385

382-
383-
384-
return promise;
385386
};
386387
}
387388
else {

package.json

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
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-
{
8-
"name": "Mike de Boer",
9-
"email": "[email protected]"
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"
1014
},
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-
]
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+
}]
4536
}

promise.js

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,17 @@
1-
/*jslint browser: true, node: true, maxlen: 128 */
2-
/*global self */
3-
41
"use strict";
52

6-
var globalObject, hasPromiseSupport;
7-
8-
function isFunction(x) {
9-
return typeof x === "function";
10-
}
3+
var Promise = global.Promise || null;
114

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;
5+
if (isFunction(Promise)) {
6+
new Promise(function(resolver) {
7+
if (!isFunction(resolver)) {
8+
Promise = null;
9+
}
10+
});
1911
}
2012

21-
hasPromiseSupport =
22-
23-
globalObject.hasOwnProperty("Promise") &&
13+
module.exports = Promise;
2414

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-
}
15+
function isFunction(x) {
16+
return typeof x === "function";
17+
}

0 commit comments

Comments
 (0)