//from callbackhell website, this is what a typical callback hell function looks like 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)) } }) }) } }) //here's another, simpler one var form = document.querySelector('form') form.onsubmit = function (submitEvent) { var name = document.querySelector('input').value request({ uri: "http://example.com/upload", body: name, method: "POST" }, function (err, response, body) { var statusMessage = document.querySelector('.status') if (err) return statusMessage.value = err statusMessage.value = body }) } //let's rewrite the simpler one document.querySelector('form').onsubmit = formSubmit; function formSubmit(submitEvent) { var name = document.querySelector('input').value request({ uri: "http://example.com/upload", body: name, method: "POST" }, postResponse) } function postResponse(err, response, body) { var statusMessage = document.querySelector('.status') if (err) return statusMessage.value = err statusMessage.value = body } //let's try it again to see how to make callbacks more understandable var form = document.querySelector('form') form.onsubmit = function (submitEvent) { var name = document.querySelector('input').value request({ uri: "http://example.com/upload", body: name, method: "POST" }, function (err, response, body) { var statusMessage = document.querySelector('.status') if (err) return statusMessage.value = err statusMessage.value = body }) } ///////////////////////////////////////////// //here's our program from before, to try it out var fs = require('fs'); fs.readdir(process.cwd(), (err, files) => { console.log(''); if(!files.length){ return console.log(' \033]31m No files to show!\033[39m\n'); } console.log(' Select which file or directory you want to see\n'); function file(i){ var filename = files[i]; fs.stat(__dirname + '/' + filename, (err, stat) => { if(stat.isDirectory()){ console.log(' '+ i +' \033[36m' + filename + '/\033[39m'); } else { console.log(' '+ i +' \033[90m'+ filename +'\033[39m'); } i++; if(i == files.length){ console.log(''); process.stdout.write(' \033[33mEnter your choice: \033[39m'); process.stdin.resume(); } else { file(i); } }); } file(0); }); //let's name the function var fs = require('fs'); fs.readdir(process.cwd(), (err, files) => { console.log(''); if(!files.length){ return console.log(' \033]31m No files to show!\033[39m\n'); } console.log(' Select which file or directory you want to see\n'); function file(i){ var filename = files[i]; fs.stat(__dirname + '/' + filename, (err, stat) => { if(stat.isDirectory()){ console.log(' '+ i +' \033[36m' + filename + '/\033[39m'); } else { console.log(' '+ i +' \033[90m'+ filename +'\033[39m'); } i++; if(i == files.length){ console.log(''); process.stdout.write(' \033[33mEnter your choice: \033[39m'); process.stdin.resume(); } else { file(i); } }); } file(0); });