Created
February 12, 2013 20:20
-
-
Save PaulCampbell/4773019 to your computer and use it in GitHub Desktop.
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
Having a wee bit o trouble with the async paradigm? |
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
var twit = new twitter(); | |
twit.get ('/statuses/show/301416893036761088.json', function(tweet){ | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.end(tweet.text); | |
}) |
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
twit = @new twitter() | |
tweet = twit.get! ('/statuses/show/301416893036761088.json') | |
response.writeHead (200, 'Content-Type': 'text/plain') | |
response.end (tweet.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pogoscript has a calling convention for async functions, taken from the Node.js fs module, many other libraries use it, but not all. The callback is called like this:
So
The problem below is that the twitter API doesn't follow this convention (rightly or wrongly) but the effect is that pogoscript thinks that the
tweet
is the error, not the result. What you'd need to do is wraptwit.get
to follow the convention:Then you can call it using pogoscript's async notation:
There's pretty scant documentation about this so I've been meaning write more examples.
Actually you can make your code a bit more robust with a
try catch
...Instead of:
Which may result in no response being sent if there is an error, you can try this:
The idea with this is that you correctly write a response regardless of whether your action succeeds or fails. All exceptions are handled for you.
Once you've wrapped some of the more esoteric async interfaces in Node land your code ends up being much easier to read and write.