Last active
June 27, 2019 07:22
-
-
Save mserranom/61b8f3a293aac2b6ed44676c07996f99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- VERBOSE CALLBACK STACK ---- | |
// | |
// This is the kind of multiple-logging situations that we're trying to avoid, | |
// as described in https://github.com/overleaf/web-internal/issues/1917. | |
// | |
// In order to remove the excessive logging, we'd need to delete log statements | |
// from lines 22, 32 and 37 (3 lines in total). | |
router.get('data', (req, res) => { | |
getData((err, data) => { | |
if(err) { | |
logger.error({ err },"couldn't fetch data") | |
return res.sendStatus(500) | |
} | |
res.send(data) | |
} | |
}) | |
function getData(callback) { | |
fetchFromDB((err, data) => { | |
if(err) { | |
logger.error({ err },'there was an error fetching from DB') | |
return callback(err) | |
} | |
return callback(data) | |
}) | |
} | |
function fetchFromDB(callback) { | |
db.connect((err, conn) => { | |
if(err) { | |
logger.error({err}, 'Connection to DB failed') | |
return callback(err) | |
} | |
conn.entity.findOne((data, err) => { | |
if(err) { | |
logger.error({ err }, "DB entity coouldn't be fetched") | |
return callback(err) | |
} | |
callback(data) | |
}) | |
}) | |
} | |
// ---- ASYNC/AWAIT --- | |
// | |
// This is a reimplementation of the code above, using promises and | |
// async/await instead | |
router.get('data', async (req, res) => { | |
try { | |
res.send(await getData()) | |
} catch { | |
logger.error({ err },"couldn't fetch data") | |
res.sendStatus(500) | |
} | |
}) | |
async function getData() { | |
try { | |
return await fetchFromDB() | |
} catch(err) { | |
logger.error({ err },'there was an error fetching from DB') | |
throw err | |
} | |
} | |
function fetchFromDB() { | |
let conn | |
try { | |
conn = await db.connect | |
} catch(err) { | |
logger.error({err}, 'Connection to DB failed') | |
throw err | |
} | |
try { | |
return conn.entity.findOne() | |
} catch(err) { | |
logger.error({ err }, "DB entity coouldn't be fetched") | |
throw err | |
} | |
} | |
// ---- ASYNC/AWAIT, excess of logging removed --- | |
// | |
// Removing the excessive logging results in a reduction of lines of code in | |
// more than half (from 33 to 16). | |
// | |
// The procedural style of async/await code is better suited for a | |
// natural propagation of Errors. | |
// | |
// Compared to callback-based code, in which every error must be handled individually | |
// for every async call, async/await will propagate automatically the error up in the | |
// stack of async calls. | |
router.get('data', async (req, res) => { | |
try { | |
res.send(await getData()) | |
} catch { | |
logger.error({ err },"couldn't fetch data") | |
res.sendStatus(500) | |
} | |
}) | |
async function getData() { | |
return await fetchFromDB() | |
} | |
function fetchFromDB() { | |
const conn = await db.connect | |
return conn.entity.findOne() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment