Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidchambers/5855045 to your computer and use it in GitHub Desktop.
Save davidchambers/5855045 to your computer and use it in GitHub Desktop.
# Takes a box (an array) and a function, f, which takes a continuation.
# A sentinel is placed in the box, then f is invoked. When f calls its
# continuation, c, the box is inspected. Only if the sentinel is still
# in the box will c in turn call its continuation.
#
# If several asynchronous requests are made in quick succession, the
# responses may not be received in the order in which they were sent.
# This function makes it possible to define a function to be invoked
# *unless the request has been superseded by the time it completes*.
skip_continuation_if_superseded = (box, f) ->
box[0] = sentinel = {}
f (continuation) -> continuation() if box[0] is sentinel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment