Skip to content

Commit 65a10e1

Browse files
callmehiphopstephenplusplus
authored andcommitted
language: add promise support (#1706)
1 parent 63ffd6e commit 65a10e1

File tree

7 files changed

+102
-8
lines changed

7 files changed

+102
-8
lines changed

packages/language/README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ var language = require('@google-cloud/language')({
1818
keyFilename: '/path/to/keyfile.json'
1919
});
2020

21-
var language = language({
22-
projectId: 'grape-spaceship-123',
23-
keyFilename: '/path/to/keyfile.json'
24-
});
25-
2621
// Get the entities from a sentence.
2722
language.detectEntities('Stephen of Michigan!', function(err, entities) {
2823
// entities = {
@@ -65,6 +60,16 @@ document.annotate(function(err, annotations) {
6560
// ]
6661
// }
6762
});
63+
64+
// Promises are also supported by omitting callbacks.
65+
document.annotate().then(function(data) {
66+
var annotations = data[0];
67+
});
68+
69+
// It's also possible to integrate with third-party Promise libraries.
70+
var language = require('@google-cloud/language')({
71+
promise: require('bluebird')
72+
});
6873
```
6974

7075

packages/language/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@
5252
"language"
5353
],
5454
"dependencies": {
55-
"@google-cloud/common": "^0.6.0",
55+
"@google-cloud/common": "^0.7.0",
5656
"arrify": "^1.0.1",
5757
"extend": "^3.0.0",
5858
"google-gax": "^0.7.0",
59+
"google-proto-files": "^0.8.3",
5960
"is": "^3.0.1",
6061
"propprop": "^0.3.1"
6162
},

packages/language/src/document.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ Document.PART_OF_SPEECH = {
354354
* // ]
355355
* // }
356356
* });
357+
*
358+
* //-
359+
* // If the callback is omitted, we'll return a Promise.
360+
* //-
361+
* document.annotate().then(function(data) {
362+
* var annotation = data[0];
363+
* var apiResponse = data[1];
364+
* });
357365
*/
358366
Document.prototype.annotate = function(options, callback) {
359367
if (is.fn(options)) {
@@ -522,6 +530,14 @@ Document.prototype.annotate = function(options, callback) {
522530
* // ]
523531
* // }
524532
* });
533+
*
534+
* //-
535+
* // If the callback is omitted, we'll return a Promise.
536+
* //-
537+
* document.detectEntities().then(function(data) {
538+
* var entities = data[0];
539+
* var apiResponse = data[1];
540+
* });
525541
*/
526542
Document.prototype.detectEntities = function(options, callback) {
527543
if (is.fn(options)) {
@@ -588,6 +604,14 @@ Document.prototype.detectEntities = function(options, callback) {
588604
* // magnitude: 40
589605
* // }
590606
* });
607+
*
608+
* //-
609+
* // If the callback is omitted, we'll return a Promise.
610+
* //-
611+
* document.detectSentiment().then(function(data) {
612+
* var sentiment = data[0];
613+
* var apiResponse = data[1];
614+
* });
591615
*/
592616
Document.prototype.detectSentiment = function(options, callback) {
593617
if (is.fn(options)) {
@@ -754,5 +778,12 @@ Document.sortByProperty_ = function(propertyName) {
754778
};
755779
};
756780

781+
/*! Developer Documentation
782+
*
783+
* All async methods (except for streams) will return a Promise in the event
784+
* that a callback is omitted.
785+
*/
786+
common.util.promisifyAll(Document);
787+
757788
module.exports = Document;
758789

packages/language/src/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ function Language(options) {
137137
* };
138138
*
139139
* language.annotate('Hello!', options, callback);
140+
*
141+
* //-
142+
* // If the callback is omitted, we'll return a Promise.
143+
* //-
144+
* language.annotate('Hello!').then(function(data) {
145+
* var entities = data[0];
146+
* var apiResponse = data[1];
147+
* });
140148
*/
141149
Language.prototype.annotate = function(content, options, callback) {
142150
if (is.fn(options)) {
@@ -220,6 +228,14 @@ Language.prototype.annotate = function(content, options, callback) {
220228
* };
221229
*
222230
* language.detectEntities('Axel Foley is from Detroit', options, callback);
231+
*
232+
* //-
233+
* // If the callback is omitted, we'll return a Promise.
234+
* //-
235+
* language.detectEntities('Axel Foley is from Detroit').then(function(data) {
236+
* var entities = data[0];
237+
* var apiResponse = data[1];
238+
* });
223239
*/
224240
Language.prototype.detectEntities = function(content, options, callback) {
225241
if (is.fn(options)) {
@@ -294,6 +310,14 @@ Language.prototype.detectEntities = function(content, options, callback) {
294310
* };
295311
*
296312
* language.detectSentiment('Hello!', options, callback);
313+
*
314+
* //-
315+
* // If the callback is omitted, we'll return a Promise.
316+
* //-
317+
* language.detectSentiment('Hello!').then(function(data) {
318+
* var sentiment = data[0];
319+
* var apiResponse = data[1];
320+
* });
297321
*/
298322
Language.prototype.detectSentiment = function(content, options, callback) {
299323
if (is.fn(options)) {
@@ -460,5 +484,14 @@ Language.prototype.text = function(content, options) {
460484
return this.document(options);
461485
};
462486

487+
/*! Developer Documentation
488+
*
489+
* All async methods (except for streams) will return a Promise in the event
490+
* that a callback is omitted.
491+
*/
492+
common.util.promisifyAll(Language, {
493+
exclude: ['document', 'html', 'text']
494+
});
495+
463496
module.exports = Language;
464497
module.exports.v1beta1 = v1beta1;

packages/language/system-test/language.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('Language', function() {
5656
});
5757

5858
after(function(done) {
59-
GCS.getBuckets({ prefix: TESTS_PREFIX })
59+
GCS.getBucketsStream({ prefix: TESTS_PREFIX })
6060
.on('error', done)
6161
.pipe(through.obj(function(bucket, _, next) {
6262
bucket.deleteFiles({ force: true }, function(err) {

packages/language/test/document.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ var proxyquire = require('proxyquire');
2323
var util = require('@google-cloud/common').util;
2424

2525
var isCustomTypeOverride;
26+
var promisified = false;
2627
var fakeUtil = extend(true, {}, util, {
2728
isCustomType: function() {
2829
if (isCustomTypeOverride) {
2930
return isCustomTypeOverride.apply(null, arguments);
3031
}
3132

3233
return false;
34+
},
35+
promisifyAll: function(Class) {
36+
if (Class.name === 'Document') {
37+
promisified = true;
38+
}
3339
}
3440
});
3541

@@ -70,6 +76,10 @@ describe('Document', function() {
7076
assert.strictEqual(document.api, LANGUAGE.api);
7177
});
7278

79+
it('should promisify all the things', function() {
80+
assert(promisified);
81+
});
82+
7383
it('should set the correct document for inline content', function() {
7484
assert.deepEqual(document.document, {
7585
content: CONFIG,

packages/language/test/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ var extend = require('extend');
2121
var proxyquire = require('proxyquire');
2222
var util = require('@google-cloud/common').util;
2323

24-
var fakeUtil = extend(true, {}, util);
24+
var promisified = false;
25+
var fakeUtil = extend({}, util, {
26+
promisifyAll: function(Class, options) {
27+
if (Class.name !== 'Language') {
28+
return;
29+
}
30+
31+
promisified = true;
32+
assert.deepEqual(options.exclude, ['document', 'html', 'text']);
33+
}
34+
});
2535

2636
function FakeDocument() {
2737
this.calledWith_ = arguments;
@@ -60,6 +70,10 @@ describe('Language', function() {
6070
});
6171

6272
describe('instantiation', function() {
73+
it('should promisify all the things', function() {
74+
assert(promisified);
75+
});
76+
6377
it('should normalize the arguments', function() {
6478
var options = {
6579
projectId: 'project-id',

0 commit comments

Comments
 (0)