Skip to content

Instantly share code, notes, and snippets.

@lucasjellema
Created July 7, 2020 14:16
Show Gist options
  • Save lucasjellema/5fd5b786fdf1c3f492d0aadde0a74dfb to your computer and use it in GitHub Desktop.
Save lucasjellema/5fd5b786fdf1c3f492d0aadde0a74dfb to your computer and use it in GitHub Desktop.
const debug = function (debugBuffer, msg) {
// using the full UTC string is a bit heavy handed perhaps; only minutes, seconds and ms would be quite enough, or just the timestamp in ms
debugBuffer.push(`${new Date().toUTCString()} ${msg}`)
}
const spillDebugBeans = function (debugBuffer) {
debugBuffer.forEach(msg => { console.log(`DEBUG: ${msg}`) });
}// spillDebugBeans
const calculateSums = async function (sums) {
const dbg = [`function calculateSums for ${JSON.stringify(sums)}`]
const results = []
try {
sums.forEach(sum => {
debug(dbg, `Sum to process ${sum}`)
const result = eval(sum)
debug(dbg, `Result from processing ${sum} = ${result}`)
results.push({ "sum": sum, "result": result })
})// forEach
}
catch (e) {
// now that things have gone sideways, please report the messages that under normal conditions would be discarded
debug(dbg, `Exception occurred ${e}`)
spillDebugBeans(dbg)
throw (e)
}
return results
} //calculateSums
const calculator = async function () {
const dbg = [`function calculator`]
try {
debug(dbg, `Sums are prepared`)
const sums = [["3+9", "2/7"], ["8*6", "2/0", "hgah"]]
debug(dbg, `Sums to process - call calulcateSums for both subsets ${JSON.stringify(sums)}`)
const results1 = await calculateSums(sums[0])
console.log(`Done processing of first bunch of sums; here are the results ${JSON.stringify(results1)}`)
const results2 = await calculateSums(sums[1])
console.log(`Done processing of second bunch of sums; here are the results ${JSON.stringify(results2)}`)
}
catch (e) {
// now that things have gone sideways, please report the messages that under normal conditions would be discarded
debug(dbg, `Exception occurred (and we are too lazy to do proper handling) ${e}`)
spillDebugBeans(dbg)
// handle exception - or not as in this case
}
} //calculator
calculator()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment