Skip to content

Commit

Permalink
docs(samples): updated samples code to use async await (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenqlogic authored and NimJay committed Nov 19, 2022
1 parent 9fc4c0c commit 8844503
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions dataproc/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,58 @@
*/

'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);
let nextRequest = request;
// Or obtain the paged response.
const options = {autoPaginate: false};
do {
const responses = await client.listClusters(nextRequest, options);
// The actual resources in a response.
const resources = responses[0];
// The next request if the response shows that there are more responses.
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]);
}
} while (nextRequest);

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);

0 comments on commit 8844503

Please sign in to comment.