Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
docs(samples): samples to use async/await (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
muraliQlogic authored and JustinBeckwith committed Nov 21, 2018
1 parent 04fa193 commit 818f10d
Show file tree
Hide file tree
Showing 9 changed files with 681 additions and 843 deletions.
114 changes: 55 additions & 59 deletions samples/deid.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

'use strict';

function deidentifyWithMask(
async function deidentifyWithMask(
callingProjectId,
string,
maskingCharacter,
Expand Down Expand Up @@ -62,20 +62,19 @@ function deidentifyWithMask(
item: item,
};

// Run deidentification request
dlp
.deidentifyContent(request)
.then(response => {
const deidentifiedItem = response[0].item;
console.log(deidentifiedItem.value);
})
.catch(err => {
console.log(`Error in deidentifyWithMask: ${err.message || err}`);
});
try {
// Run deidentification request
const [response] = await dlp.deidentifyContent(request);
const deidentifiedItem = response.item;
console.log(deidentifiedItem.value);
} catch (err) {
console.log(`Error in deidentifyWithMask: ${err.message || err}`);
}

// [END dlp_deidentify_masking]
}

function deidentifyWithDateShift(
async function deidentifyWithDateShift(
callingProjectId,
inputCsvFile,
outputCsvFile,
Expand Down Expand Up @@ -208,36 +207,35 @@ function deidentifyWithDateShift(
item: tableItem,
};

// Run deidentification request
dlp
.deidentifyContent(request)
.then(response => {
const tableRows = response[0].item.table.rows;

// Write results to a CSV file
tableRows.forEach((row, rowIndex) => {
const rowValues = row.values.map(
value =>
value.stringValue ||
`${value.dateValue.month}/${value.dateValue.day}/${
value.dateValue.year
}`
);
csvLines[rowIndex + 1] = rowValues.join(',');
});
csvLines.push('');
fs.writeFileSync(outputCsvFile, csvLines.join('\n'));

// Print status
console.log(`Successfully saved date-shift output to ${outputCsvFile}`);
})
.catch(err => {
console.log(`Error in deidentifyWithDateShift: ${err.message || err}`);
try {
// Run deidentification request
const [response] = await dlp.deidentifyContent(request);
const tableRows = response.item.table.rows;

// Write results to a CSV file
tableRows.forEach((row, rowIndex) => {
const rowValues = row.values.map(
value =>
value.stringValue ||
`${value.dateValue.month}/${value.dateValue.day}/${
value.dateValue.year
}`
);
csvLines[rowIndex + 1] = rowValues.join(',');
});
csvLines.push('');
fs.writeFileSync(outputCsvFile, csvLines.join('\n'));

// Print status
console.log(`Successfully saved date-shift output to ${outputCsvFile}`);
} catch (err) {
console.log(`Error in deidentifyWithDateShift: ${err.message || err}`);
}

// [END dlp_deidentify_date_shift]
}

function deidentifyWithFpe(
async function deidentifyWithFpe(
callingProjectId,
string,
alphabet,
Expand Down Expand Up @@ -309,20 +307,19 @@ function deidentifyWithFpe(
item: item,
};

// Run deidentification request
dlp
.deidentifyContent(request)
.then(response => {
const deidentifiedItem = response[0].item;
console.log(deidentifiedItem.value);
})
.catch(err => {
console.log(`Error in deidentifyWithFpe: ${err.message || err}`);
});
try {
// Run deidentification request
const [response] = await dlp.deidentifyContent(request);
const deidentifiedItem = response.item;
console.log(deidentifiedItem.value);
} catch (err) {
console.log(`Error in deidentifyWithFpe: ${err.message || err}`);
}

// [END dlp_deidentify_fpe]
}

function reidentifyWithFpe(
async function reidentifyWithFpe(
callingProjectId,
string,
alphabet,
Expand Down Expand Up @@ -396,16 +393,15 @@ function reidentifyWithFpe(
item: item,
};

// Run reidentification request
dlp
.reidentifyContent(request)
.then(response => {
const reidentifiedItem = response[0].item;
console.log(reidentifiedItem.value);
})
.catch(err => {
console.log(`Error in reidentifyWithFpe: ${err.message || err}`);
});
try {
// Run reidentification request
const [response] = await dlp.reidentifyContent(request);
const reidentifiedItem = response.item;
console.log(reidentifiedItem.value);
} catch (err) {
console.log(`Error in reidentifyWithFpe: ${err.message || err}`);
}

// [END dlp_reidentify_fpe]
}

Expand Down
Loading

0 comments on commit 818f10d

Please sign in to comment.