Skip to content

Instantly share code, notes, and snippets.

@misterdai
Created October 16, 2013 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterdai/7006043 to your computer and use it in GitHub Desktop.
Save misterdai/7006043 to your computer and use it in GitHub Desktop.
Considering approaches of laying out code and handling callbacks. Which one would you pick? getData1, getData2 or getData3?... Or some other method?
request = require 'request'
class Test
_processData: (details, callback, error, response, body) =>
if error?
return callback error
if reponse.statusCode isnt 200
return callback(new Error(response.statusCode))
try
callback null, {data: JSON.parse(body), details: details}
catch error
callback error
getData1: (details, callback) =>
request details.url, @_processData.bind(@, details, callback)
# ----- OR ------
getData2: (details, callback) =>
request details.url, (error, response, body) =>
@_processData(details, callback, error, response, body)
# ----- OR -----
getData3: (details, callback) =>
request details.url, (error, response, body) =>
if error?
return callback error
if reponse.statusCode isnt 200
return callback(new Error(response.statusCode))
try
callback null, {data: JSON.parse(body), details: details}
catch error
callback error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment