google-cloud v0.42.2
Updating
$ npm install google-cloud
Hello, Promises!
It's been a long time coming, but we're finally here. We've shipped promise support in the Google Cloud!
Do I have to use promises?
Nope, carry on. (But keep reading if you use streams)
How do I use promises?
Don't pass a callback:
var gcloud = require('google-cloud')();
var gcs = gcloud.storage();
gcs.getBuckets()
.then(function(data) {
var buckets = data[0];
})
.catch(function(err) {});
Why do promises return an array?
I'm glad you asked! A promise can only be resolved with a single value, which presented a problem for us since many of our methods return multiple values. The choice to use an array was made to benefit users who are using third-party Promise modules or running Node >= 6.
Node >= 6 users can use destructuring:
storage.getBuckets({
autoPaginate: false
})
.then(([buckets, nextQuery, apiResponse]) => {
// Destructuring to the rescue!
})
.catch(function(err) {});
Bluebird and Q users can use the spread
method:
storage.getBuckets({
autoPaginate: false
})
.spread(function(buckets, nextQuery, apiResponse) {
// spread to the rescue!
})
.catch(function(err) {};
How do I use a third-party promise module with google-cloud
?
You can specify an ES6-compatible Promise module via the promise
option.
// Bluebird will be used throughout the entire API.
var gcloud = require('google-cloud')({
promise: require('bluebird')
});
// Bluebird will be used for just the Storage API.
var gcloud = require('google-cloud')();
var gcs = gcloud.storage({
promise: require('bluebird')
});
⚠️ What happened to the streaming functionality?
In previous releases of google-cloud
, some methods returned a stream if a callback was omitted. In order to have streams and promises live together, all streaming functionality has moved to their own methods.
Here's a list of release notes for affected modules.
Features
⚠️ Datastore (#1682) - Allow using a Symbol to retrieve a key from an Entity.⚠️ Logging (#1666) - Change resource argument to metadata object. (Thanks @zbjornson!)⚠️ PubSub (#1662) - Allow simple publishing.- Bigtable (#1567) - Add support for emulator via
BIGTABLE_EMULATOR_HOST
environment variable. - Translate (#1674) - Add support for
GOOGLE_CLOUD_TRANSLATE_ENDPOINT
environment variable. - Translate (#1647) - Add support for translating HTML. (Thanks @innovative1!)