A finite state machine implemented using jQuery Deferred