Refactor async#11
Open
eschwartz wants to merge 12 commits into
Open
Conversation
added 12 commits
September 7, 2016 12:09
previously this would cause an uncaughtException, because the TypeError was not passed to the callback
was not resetting before each test
- prevent uncaught exceptions (co treats sync and async exceptions the same) - improve readability (escape from callback hell)
no longer used
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request includes no functional changes. However, it does drop support for Node 0.x.
I have refactored the code using
coto better control the flow of asynchronous code.My primary motivation for using
cois to improve error handling. In callback-style asynchronous code, it's really easy to swallow exceptions. For example:Neither of the errors above are passed to
callback, which means that whoever is callinginfocannot catch them.You can wrap everything in
try/catchblocks, but this starts to get really messy for deep-nested code.I looked at using
async, which has the advantage of supporting Node 0.x, and it's already a dependency. However, it comes with some of the same problems, and some new ones.Consider:
co, on the other hand, treats async and sync errors the same, so you don't need to worry about uncaught exceptions. It also allows you to keep everything in the same scope, making reasoning about your code much easier.The syntax is very similar to the proposed
async/awaitsyntaxAs I noted in pull request #10, I'm trying to debug timeouts in my tile server, and I'm suspecting that some of the timeouts are caused by uncaught exceptions in upstream code (including tilelive-tmstyle). I'm hoping these changes will allow me to catch, log, and fix some of these errors.