From 8554fece04249295867aabe3401017f980cf8aac Mon Sep 17 00:00:00 2001 From: locle-groove <95837097+locle-groove@users.noreply.github.com> Date: Tue, 23 Aug 2022 11:26:37 +0700 Subject: [PATCH] Update documentation.md --- documentation.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/documentation.md b/documentation.md index 7a6812c..81597a4 100644 --- a/documentation.md +++ b/documentation.md @@ -85,6 +85,43 @@ function ProfileListScreen() { - Sentry +### 1.7. CallBack Hell + + Callback hell also known as pyramid of doom, hadouken is a way to code suboptimal + + For example + ``` + fs.readdir(source, function (err, files) { + if (err) { + console.log('Error finding files: ' + err) + } else { + files.forEach(function (filename, fileIndex) { + console.log(filename) + gm(source + filename).size(function (err, values) { + if (err) { + console.log('Error identifying file size: ' + err) + } else { + console.log(filename + ' : ' + values) + aspect = (values.width / values.height) + widths.forEach(function (width, widthIndex) { + height = Math.round(width / aspect) + console.log('resizing ' + filename + 'to ' + height + 'x' + height) + this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) { + if (err) console.log('Error writing file: ' + err) + }) + }.bind(this)) + } + }) + }) + } +}) + ``` +* How do I fix callback hell? +- Keep your code shallow +- Modularize +- Handle every single error + + ### 2. Memoize expensive computations with React Hooks #### 2.1. useMemo