Skip to content

Commit 9c59736

Browse files
committed
storage: add promise support
1 parent 4374e6f commit 9c59736

13 files changed

Lines changed: 601 additions & 120 deletions

File tree

packages/storage/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ remoteReadStream.pipe(localWriteStream);
4848
var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
4949
var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
5050
localReadStream.pipe(remoteWriteStream);
51+
52+
// Promises are also supported by omitting callbacks.
53+
bucket.upload('/photos/zoo/zebra.jpg').then(function(data) {
54+
var file = data[0];
55+
});
5156
```
5257

5358

packages/storage/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"storage"
5151
],
5252
"dependencies": {
53-
"@google-cloud/common": "^0.6.0",
53+
"@google-cloud/common": "^0.7.0",
5454
"arrify": "^1.0.0",
5555
"async": "^2.0.1",
5656
"concat-stream": "^1.5.0",

packages/storage/src/acl.js

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'use strict';
2222

2323
var arrify = require('arrify');
24+
var common = require('@google-cloud/common');
2425
var is = require('is');
2526
var util = require('util');
2627

@@ -107,6 +108,14 @@ function Acl(options) {
107108
* entity: 'user-email@example.com',
108109
* role: gcs.acl.OWNER_ROLE
109110
* }, function(err, aclObject) {});
111+
*
112+
* //-
113+
* // If the callback is omitted, we'll return a Promise.
114+
* //-
115+
* myFile.acl.owners.addUser('[email protected]').then(function(data) {
116+
* var aclObject = data[0];
117+
* var apiResponse = data[1];
118+
* });
110119
*/
111120
Acl.prototype.owners = {};
112121

@@ -144,6 +153,14 @@ Acl.prototype.owners = {};
144153
* entity: 'user-email@example.com',
145154
* role: gcs.acl.READER_ROLE
146155
* }, function(err, aclObject) {});
156+
*
157+
* //-
158+
* // If the callback is omitted, we'll return a Promise.
159+
* //-
160+
* myFile.acl.readers.addUser('[email protected]').then(function(data) {
161+
* var aclObject = data[0];
162+
* var apiResponse = data[1];
163+
* });
147164
*/
148165
Acl.prototype.readers = {};
149166

@@ -181,6 +198,14 @@ Acl.prototype.readers = {};
181198
* entity: 'user-email@example.com',
182199
* role: gcs.acl.WRITER_ROLE
183200
* }, function(err, aclObject) {});
201+
*
202+
* //-
203+
* // If the callback is omitted, we'll return a Promise.
204+
* //-
205+
* myFile.acl.writers.addUser('[email protected]').then(function(data) {
206+
* var aclObject = data[0];
207+
* var apiResponse = data[1];
208+
* });
184209
*/
185210
Acl.prototype.writers = {};
186211

@@ -204,10 +229,12 @@ util.inherits(Acl, AclRoleAccessorMethods);
204229
* @param {object} callback.apiResponse - The full API response.
205230
*
206231
* @example
207-
* myBucket.acl.add({
232+
* var options = {
208233
* entity: 'user-useremail@example.com',
209234
* role: gcs.acl.OWNER_ROLE
210-
* }, function(err, aclObject, apiResponse) {});
235+
* };
236+
*
237+
* myBucket.acl.add(options, function(err, aclObject, apiResponse) {});
211238
*
212239
* //-
213240
* // For file ACL operations, you can also specify a `generation` property.
@@ -219,6 +246,14 @@ util.inherits(Acl, AclRoleAccessorMethods);
219246
* role: gcs.acl.OWNER_ROLE,
220247
* generation: 1
221248
* }, function(err, aclObject, apiResponse) {});
249+
*
250+
* //-
251+
* // If the callback is omitted, we'll return a Promise.
252+
* //-
253+
* myBucket.acl.add(options).then(function(data) {
254+
* var aclObject = data[0];
255+
* var apiResponse = data[1];
256+
* });
222257
*/
223258
Acl.prototype.add = function(options, callback) {
224259
var self = this;
@@ -273,6 +308,13 @@ Acl.prototype.add = function(options, callback) {
273308
* entity: 'user-useremail@example.com',
274309
* generation: 1
275310
* }, function(err, apiResponse) {});
311+
*
312+
* //-
313+
* // If the callback is omitted, we'll return a Promise.
314+
* //-
315+
* myFile.acl.delete().then(function(data) {
316+
* var apiResponse = data[0];
317+
* });
276318
*/
277319
Acl.prototype.delete = function(options, callback) {
278320
var query = {};
@@ -333,6 +375,14 @@ Acl.prototype.delete = function(options, callback) {
333375
* entity: 'user-useremail@example.com',
334376
* generation: 1
335377
* }, function(err, aclObject, apiResponse) {});
378+
*
379+
* //-
380+
* // If the callback is omitted, we'll return a Promise.
381+
* //-
382+
* myBucket.acl.get().then(function(data) {
383+
* var aclObject = data[0];
384+
* var apiResponse = data[1];
385+
* });
336386
*/
337387
Acl.prototype.get = function(options, callback) {
338388
var self = this;
@@ -389,10 +439,12 @@ Acl.prototype.get = function(options, callback) {
389439
* @param {object} callback.apiResponse - The full API response.
390440
*
391441
* @example
392-
* myBucket.acl.update({
442+
* var options = {
393443
* entity: 'user-useremail@example.com',
394444
* role: gcs.acl.WRITER_ROLE
395-
* }, function(err, aclObject, apiResponse) {});
445+
* };
446+
*
447+
* myBucket.acl.update(options, function(err, aclObject, apiResponse) {});
396448
*
397449
* //-
398450
* // For file ACL operations, you can also specify a `generation` property.
@@ -402,6 +454,14 @@ Acl.prototype.get = function(options, callback) {
402454
* role: gcs.acl.WRITER_ROLE,
403455
* generation: 1
404456
* }, function(err, aclObject, apiResponse) {});
457+
*
458+
* //-
459+
* // If the callback is omitted, we'll return a Promise.
460+
* //-
461+
* myFile.acl.update(options).then(function(data) {
462+
* var aclObject = data[0];
463+
* var apiResponse = data[1];
464+
* });
405465
*/
406466
Acl.prototype.update = function(options, callback) {
407467
var self = this;
@@ -463,6 +523,13 @@ Acl.prototype.request = function(reqOpts, callback) {
463523
this.request_(reqOpts, callback);
464524
};
465525

526+
/*! Developer Documentation
527+
*
528+
* All async methods (except for streams) will return a Promise in the event
529+
* that a callback is omitted.
530+
*/
531+
common.util.promisifyAll(Acl);
532+
466533
module.exports = Acl;
467534

468535
/**
@@ -539,7 +606,7 @@ AclRoleAccessorMethods.prototype._assignAccessMethods = function(role) {
539606
callback = entityId;
540607
}
541608

542-
self[accessMethod]({
609+
return self[accessMethod]({
543610
entity: apiEntity,
544611
role: role
545612
}, callback);

0 commit comments

Comments
 (0)