November 2012
Intermediate to advanced
106 pages
2h 20m
English
The most common anti-pattern in JavaScript is nesting callbacks within callbacks. Remember the Pyramid of Doom from the introduction? Let’s look at a concrete example that you might see in a Node server.
| | function checkPassword(username, passwordGuess, callback) { |
| | var queryStr = 'SELECT * FROM user WHERE username = ?'; |
| | db.query(selectUser, username, function (err, result) { |
| | if (err) throw err; |
| | hash(passwordGuess, function(passwordGuessHash) { |
| | callback(passwordGuessHash === result['password_hash']); |
| | }); |
| | }); |
| | } |
Here we’ve defined an async function (checkPassword), which fires another async function (db.query), which potentially fires another async function (hash). (It’s impossible to know for ...
Read now
Unlock full access