From 8844503de270261bc28ba8fab1a89330a475b203 Mon Sep 17 00:00:00 2001 From: praveenqlogic <44371467+praveenqlogic@users.noreply.github.com> Date: Thu, 22 Nov 2018 23:45:42 +0530 Subject: [PATCH] docs(samples): updated samples code to use async await (#120) --- dataproc/quickstart.js | 90 +++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/dataproc/quickstart.js b/dataproc/quickstart.js index f86331dac7..f2a3098a41 100644 --- a/dataproc/quickstart.js +++ b/dataproc/quickstart.js @@ -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= GOOGLE_APPLICATION_CREDENTIALS= node #{$0}' + ); + } -// [START dataproc_quickstart] -if ( - !process.env.GCLOUD_PROJECT || - !process.env.GOOGLE_APPLICATION_CREDENTIALS -) { - throw new Error( - 'Usage: GCLOUD_PROJECT= GOOGLE_APPLICATION_CREDENTIALS= 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);