r/programming • • Nov 02 '12

Escape from Callback Hell: Callbacks are the modern goto

http://elm-lang.org/learn/Escape-from-Callback-Hell.elm
607 Upvotes

414 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Nov 02 '12

Could you explain how to use a state machine to do the Flickr instant search example? I'm not really sure what you mean by saying state machines are the solution here.

3

u/willcode4beer Nov 02 '12 edited Nov 03 '12

Let's make up some states. For example, (this is off the top of my head so, feel free to improve) let's say WAITING, GETTING_PHOTOS, GETTING_OPTIONS. Let's add some events: USER_ENTERS_TAG, PHOTOS_RETURNED, OPTIONS_RETURNED

Now, let's make a little table:

XWAITINGGETTING_PHOTOSGETTING_OPTIONS
USER_ENTERS_TAG???
PHOTOS_RETURNED???
OPTIONS_RETURNED???

.

.

Now, define what should happen for each event for each state. For example:

events/statesWAITINGGETTING_PHOTOSGETTING_OPTIONS
USER_ENTERS_TAGget photos, change state to GETTING_PHOTOSget new photos, change state to GETTING_PHOTOSget new photos, change state to GETTING_PHOTOS
PHOTOS_RETURNEDignore?start getting options, change state to GETTING_OPTIONSstart getting options
OPTIONS_RETURNEDignore?ignore?render options

3

u/[deleted] Nov 03 '12 edited Nov 03 '12

This seems to me like a clear solution, but also one that could potentially scale very poorly. I haven't implemented any large web applications explicitly as a finite state machine, but it seems like the states could increase in number exponentially. It seems like you'd really need some sort of library dedicated to this to help the FSM scale. Plus you'd probably need multiple FSM's for different parts of the app, and so on.

In short, I think this could be made to work, perhaps quite well, but I'm not convinced it's necessarily better than what the OP is proposing with Elm. Granted, I haven't written a large-scale application with Elm either, but I haven't seen anything so far that would prevent it from scaling.

Edit: I'm checking out the tutorials you linked to in another post.

-5

u/runvnc Nov 03 '12

LOL.

Its 12 lines of CoffeeScript (counting the blank ones), 8 without the blank lines.

flickr = 'http://api.flickr.com/services/rest/?format=json&nojsoncallback=1&api_key=256663858aa10e52a838a58b7866d858'

showPhoto = (p) ->
  $('#result').attr 'src', "http://farm#{p.farm}.staticflickr.com/#{p.server}/#{p.id}_#{p.secret}.jpg"
  
searchTag = (tag) ->
  $.get "#{flickr}&method=flickr.photos.search&sort=random&per_page=10&tags=#{tag}", (results) ->
    n = Math.floor Math.random() * (results?.photos?.photo.length-1)
    showPhoto results?.photos?.photo[n]
        
$('#tag').keydown ->
  setTimeout (-> searchTag $('#tag').val()), 10

http://jsfiddle.net/3KzB9/28/