Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Off the Rails

Off the Rails

My journey to becoming a
~hipster~ Node.js developer

Avatar for Danielle Smith

Danielle Smith

March 17, 2017
Tweet

More Decks by Danielle Smith

Other Decks in Programming

Transcript

  1. Developer B: “where is that going to show up?” Developer

    A: “in the server log” Developer B: “okay cool”
  2. Developer B: “where is that going to show up?” Developer

    A: “in the server log” Developer B: “okay cool” both devs watch the server log for some time
  3. Developer B: “where is that going to show up?” Developer

    A: “in the server log” Developer B: “okay cool” both devs watch the server log for some time Developer A: “No wait, there it is in Chrome’s devtools"
  4. The anatomy of an ‘errorback’ function(error, results…) this is non-null

    if there was an error the result(s) of the function taking the callback
  5. foo(42, function(err, result) { // happens “later” if (err) {

    console.error(err.message); } else { console.log(result); } }); // happens immediately ...
  6. foo(42, function(err, fooResult) { if (!err) { bar(fooResult, function(err, barResult)

    { if (!err) { baz(barResult, function(err, bazResult) { if (!err) { qux(bazResult, function(err, quxResult) { if (!err) { norf(quxResult, function(err, norfResult) { if (!err) { console.log("Great Success!"); } else { console.error("Could not norf"); } }) } else console.error("Could not qux"); }); } else console.error("Could not baz"); }); } else console.error("Could not bar"); }); } else console.error("Could not foo"); });
  7. async.waterfall([ function(cb) { foo(42, cb); }, bar, baz, qux, norf

    ], function(err, result) { if (!err) { console.log("Great Success!"); } else { console.error("Could not norf: " + err.message); } });
  8. foo(42) .then(bar) .then(baz) .then(qux) .then(norf) .then(function(norfResult) { console.log("Great Success!"); })

    .catch(function(error) { console.error("Could not norf: " + err.message); });
  9. User.find(42).then(function(user) { return user.getPosts(); }).filter(function(post) { return /Lorem Ipsum/.test(post.body); }).map(function(post)

    { return post.getComments(); }).reduce(function(total, comment) { return comment.getLikes() .then(function(likes) { return total + likes.length; }); }, 0);
  10. User.find(42).then(function(user) { return user.getPosts(); }).then(function(posts) { // yay, I have

    posts! // but where is my user? }).then(...); Bringing ‘user’ to the party
  11. var user; User.find(42).then(function(_user_) { user = _user_; return user.getPosts() }).then(function(posts)

    { // yay, I have posts! // and I have a user! }).then(...); Klutzy variables?
  12. var findUser = User.find(42); findUser.then(function(user) { return user.getPosts(); }).then(function(posts) {

    var user = findUser.value(); // yay, I have posts! // and I have a user! }).then(...); ‘Resolved’ values?
  13. var nizzleUserPosts = function(user) { return user.getPosts() .then(function(posts) { //

    yay, I have posts! // and I have a user! }); } User.find(42).then(nizzleUserPosts) .then(...); Refactor that Shizz-Nizzle!
  14. The WTFs of Asynchronous-ness “I called this here and it

    console.log’s there, wtf?” “Foo is happening before Bar, wtf?” “I called Bar, but it never happened, wtf?” “I never called Baz, but it’s happening, wtf?”