Skip to content

Commit 8ff93d0

Browse files
callmehiphopstephenplusplus
authored andcommitted
speech: add promise support (#1710)
1 parent e6f2059 commit 8ff93d0

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

packages/google-cloud-speech/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ fs.createReadStream('./audio.raw')
5858
// ...
5959
// }
6060
});
61+
62+
// Promises are also supported by omitting callbacks.
63+
speech.recognize('./audio.raw', {
64+
encoding: 'LINEAR16',
65+
sampleRate: 16000
66+
}).then(function(data) {
67+
var transcript = data[0];
68+
});
69+
70+
// It's also possible to integrate with third-party Promise libraries.
71+
var speech = require('@google-cloud/speech')({
72+
promise: require('bluebird')
73+
});
6174
```
6275

6376

packages/google-cloud-speech/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"speech"
5555
],
5656
"dependencies": {
57-
"@google-cloud/common": "^0.6.0",
57+
"@google-cloud/common": "^0.7.0",
5858
"events-intercept": "^2.0.0",
5959
"extend": "^3.0.0",
6060
"google-gax": "^0.7.0",

packages/google-cloud-speech/src/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,14 @@ Speech.prototype.operation = function(name) {
632632
* // }
633633
* // ]
634634
* });
635+
*
636+
* //-
637+
* // If the callback is omitted, we'll return a Promise.
638+
* //-
639+
* speech.recognize('./bridge.raw', config).then(function(data) {
640+
* var results = data[0];
641+
* var apiResponse = data[1];
642+
* });
635643
*/
636644
Speech.prototype.recognize = function(file, config, callback) {
637645
var self = this;
@@ -764,6 +772,14 @@ Speech.prototype.recognize = function(file, config, callback) {
764772
* // ]
765773
* });
766774
* });
775+
*
776+
* //-
777+
* // If the callback is omitted, we'll return a Promise.
778+
* //-
779+
* speech.startRecognition('./bridge.raw', config).then(function(data) {
780+
* var operation = data[0];
781+
* var apiResponse = data[1];
782+
* });
767783
*/
768784
Speech.prototype.startRecognition = function(file, config, callback) {
769785
var self = this;
@@ -811,5 +827,14 @@ Speech.prototype.startRecognition = function(file, config, callback) {
811827
});
812828
};
813829

830+
/*! Developer Documentation
831+
*
832+
* All async methods (except for streams) will return a Promise in the event
833+
* that a callback is omitted.
834+
*/
835+
common.util.promisifyAll(Speech, {
836+
exclude: ['operation']
837+
});
838+
814839
module.exports = Speech;
815840
module.exports.v1beta1 = v1beta1;

packages/google-cloud-speech/test/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ var tmp = require('tmp');
2727

2828
var util = require('@google-cloud/common').util;
2929

30-
var fakeUtil = extend({}, util);
30+
var promisified = false;
31+
var fakeUtil = extend({}, util, {
32+
promisifyAll: function(Class, options) {
33+
if (Class.name !== 'Speech') {
34+
return;
35+
}
36+
37+
promisified = true;
38+
assert.deepEqual(options.exclude, ['operation']);
39+
}
40+
});
3141

3242
function FakeGrpcOperation() {
3343
this.calledWith_ = arguments;
@@ -90,6 +100,10 @@ describe('Speech', function() {
90100
});
91101

92102
describe('instantiation', function() {
103+
it('should promisify all the things', function() {
104+
assert(promisified);
105+
});
106+
93107
it('should normalize the arguments', function() {
94108
var normalizeArguments = fakeUtil.normalizeArguments;
95109
var normalizeArgumentsCalled = false;

0 commit comments

Comments
 (0)