Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(samples): updated samples code to use async await #1

Merged
merged 7 commits into from
Dec 11, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 48 additions & 46 deletions samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,62 @@
*/

'use strict';
async function main() {
// [START dataproc_quickstart]
if (
!process.env.GCLOUD_PROJECT ||
!process.env.GOOGLE_APPLICATION_CREDENTIALS
) {
throw new Error(
'Usage: GCLOUD_PROJECT=<project_id> GOOGLE_APPLICATION_CREDENTIALS=<path to json key> node #{$0}'
);
}

// [START dataproc_quickstart]
if (
!process.env.GCLOUD_PROJECT ||
!process.env.GOOGLE_APPLICATION_CREDENTIALS
) {
throw new Error(
'Usage: GCLOUD_PROJECT=<project_id> GOOGLE_APPLICATION_CREDENTIALS=<path to json key> node #{$0}'
);
}

const dataproc = require('@google-cloud/dataproc');
const dataproc = require('@google-cloud/dataproc');

const client = new dataproc.v1.ClusterControllerClient({
// optional auth parameters.
});
const client = new dataproc.v1.ClusterControllerClient({
// optional auth parameters.
});

const projectId = process.env.GCLOUD_PROJECT;
const projectId = process.env.GCLOUD_PROJECT;

// Iterate over all elements.
const region = 'global';
const request = {
projectId: projectId,
region: region,
};
// Iterate over all elements.
const region = 'global';
const request = {
projectId: projectId,
region: region,
};

client.listClusters(request).then(responses => {
const resources = responses[0];
const [resources] = await client.listClusters(request);
console.log('Total resources:', resources.length);
for (let i = 0; i < resources.length; i += 1) {
console.log(resources[i]);
}
});

// Or obtain the paged response.
const options = {autoPaginate: false};
const callback = responses => {
// The actual resources in a response.
const resources = responses[0];
// The next request if the response shows that there are more responses.
const nextRequest = responses[1];
// The actual response object, if necessary.
// const rawResponse = responses[2];
for (let i = 0; i < resources.length; i += 1) {
console.log(resources[i]);
}
if (nextRequest) {
// Fetch the next page.
return client.listClusters(nextRequest, options).then(callback);
}
};
client.listClusters(request, options).then(callback);
// Or obtain the paged response.
const options = {autoPaginate: false};
const callback = async responses => {
// The actual resources in a response.
const resources = responses[0];
// The next request if the response shows that there are more responses.
const nextRequest = responses[1];
// The actual response object, if necessary.
// const rawResponse = responses[2];
for (let i = 0; i < resources.length; i += 1) {
console.log(resources[i]);
}
if (nextRequest) {
// Fetch the next page.
const response = await client.listClusters(nextRequest, options);
return callback(response);
}
};
const response = await client.listClusters(request, options);
callback(response);
client.listClustersStream(request).on('data', element => {
console.log(element);
});
// [END dataproc_quickstart]
}

client.listClustersStream(request).on('data', element => {
console.log(element);
});
// [END dataproc_quickstart]
main().catch(console.error);